首页 > 解决方案 > 来自 ExpandableListView 的子元素中的 setOnClickListener

问题描述

我正在创建一个expandableListView,我想从我的expandableListView 中单击子元素中存在的元素,并调用在我的片段中声明的函数,它是我的expandableListView。我尝试了很多解决方案,但我无法获得预期的结果。我看到了 Listview 的一种解决方案,使用performItemClick(),但继续没有结果。

我的适配器 getChildView

 @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        final JSONObject expandedListText = (JSONObject) getChild(groupPosition, childPosition);
        if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_event_presented_profile, null);
        }
        try {
            ...  
            //element it's un image 
            convertView.findViewById(R.id.rejectEvent).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            Toast.makeText(context, "Reject " + expandedListText.getString("_id"), Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
              ...
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }

我的片段

private void expandableListView (){
     expandableListView = view.findViewById(R.id.expandableListView_events);
     adapter = new ProfileCustomExpandableListAdapter(view.getContext(), titleList, this.meetingsMapJsonObj, userAccount,expandableListView);
            expandableListView.setAdapter(adapter);
            expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                    /*try {
                        Toast.makeText(view.getContext(), meetingsMapJsonObj.get(titleList.get(groupPosition)).get(childPosition).getString("_id"), Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }*/

                    long viewId = view.getId();
                    if (viewId == R.id.rejectEvent) {
                        Toast.makeText(getContext(), "reject", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(getContext(), "ListView clicked" + id, Toast.LENGTH_SHORT).show();
                    }
                    return false;
                }
            });
}

我真的很想点击rejectEvent并执行以下功能。

public void leavingMeeting(int groupPosition, int childPosition, String id){
      Toast.makeText(getContext(), "Reject participation", Toast.LENGTH_SHORT).show();
}

标签: javaandroid

解决方案


推荐阅读