java - 来自 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();
}
解决方案
推荐阅读
- java - 如何在 Weka 中使用 .arff 文件预测未标记的数据?
- bash - 为什么我在 /etc/init.d 中的脚本在 rc0.d 和 rc6.d 中带有 K 符号链接,在 rc5.d 中带有 S 符号链接在关机时不起作用?
- javascript - 如何在 JSX 中打印 props 对象,我收到 TypeError: Cannot read property of undefined
- angular - 如何通过单击 ag-grid 中的列标题来获取列名
- nested - Rails 有两个嵌套属性,“posts”和“issues”上的“comments” Not Saving Rollback
- cplex - 如何在 CPLEX OPL 中编写平均约束?
- c# - 讲述人未按预期读取 TextBlock 中的文本。仅在选定的 TextBox 中
- python - 是否可以在 python 中的函数上使用模式匹配?比如,一个可以响应多个调用的函数
- php - 如何更新数据库中的 json 字段 laravel?
- kafka-consumer-api - 具有相同组 ID kafka 的同一应用程序的多个实例