gpt4 book ai didi

android - 如果前面的组被展开,则 Expanded ListView 的 setOnItemLongClickListener 给出不正确的 itemIndex

转载 作者:行者123 更新时间:2023-11-29 22:07:52 25 4
gpt4 key购买 nike

我通过扩展 BaseExpandableListAdapter 来填充展开的 ListView 。
ChildView 的代码:

public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

LayoutInflater in=getLayoutInflater();
convertView=in.inflate(R.layout.details, null);
//set text for various TextViews from ArrayList1
return convertView;
}

GroupView 代码:

public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LayoutInflater li= getLayoutInflater();
convertView=li.inflate(R.layout.list, null);
//add data to ArrayList1 and set text for various TextViews
return convertView;
}

如果一个组被展开并且我长按任何后续组 onItemLongClick 方法获取参数 itemIndex 的(实际索引 + 1)值。因此,我得到 arrayindexoutofbounds 异常。

elview.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int itemIndex, long arg3) {
Log.e("index selected", itemIndex+"");
return false;
}});

我是不是漏掉了什么?

最佳答案

我没有看到您为上下文菜单注册适配器...

以下是我如何顺利完成的部分内容:

适配器创建

    mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
R.layout.explistlayout,
R.layout.explistlayout1,
new String[] { "_id" },
new int[] { android.R.id.text1 },
new String[] { child },
new int[] { android.R.id.text1 });
lv.setAdapter(mAdapter);
registerForContextMenu(lv);

上下文菜单和操作

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == 1) {
menu.setHeaderTitle("Event Operations");
menu.add(0, v.getId(), 0, "Edit Event");
menu.add(0, v.getId(), 0, "Delete Event");
}
}

@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
if (item.getTitle() == "Edit Event") {
mRowId = info.id;
EventAddFragment eventadd = new EventAddFragment();
getFragmentManager().beginTransaction()
.replace(R.id.rightpane, eventadd).commit();
} else if (item.getTitle() == "Delete Event") {
mDbHelper.deleteEvent(info.id);
mGroupsCursor.requery();
return true;
}
return super.onContextItemSelected(item);
}

您还可以查看 this SO question了解更多详情。

希望这对您有所帮助。

关于android - 如果前面的组被展开,则 Expanded ListView 的 setOnItemLongClickListener 给出不正确的 itemIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10369281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com