gpt4 book ai didi

android - 如何将整个对象传递给 ExpandableListView 中的 onChildClick 中的 Intent

转载 作者:太空狗 更新时间:2023-10-29 14:43:43 24 4
gpt4 key购买 nike

在 GridView 或简单的 ListView 中,我使用这段代码将一个对象传递给一个新的 Activity ,它工作得很好:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Illusion i = (Illusion) parent.getItemAtPosition(position);
Intent intent = new Intent(IllusionsGridActivity.this, ViewIllusionActivity.class);
intent.putExtra("item", i);
startActivity(intent);
}
});

我试图在可扩展列表中对子对象执行相同的操作,但它不起作用。它似乎只接受 String 值。所以这是行不通的:

listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Illusion i = (Illusion) parent.getItemAtPosition(childPosition);
Intent intent = new Intent(IllusionsListActivity.this, ViewIllusionActivity.class);
intent.putExtra("item", i);
startActivity(intent);
return false;
}
});

有没有办法将整个对象传递给此方法内的新 Activity ?

最佳答案

尝试下一个代码:

listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
long packedPosition = ExpandableListView.getPackedPositionForChild(groupPosition, childPosition);
int flatPosition = parent.getFlatListPosition(packedPosition)
Illusion i = (Illusion) parent.getItemAtPosition(flatPosition);
Intent intent = new Intent(IllusionsListActivity.this, ViewIllusionActivity.class);
intent.putExtra("item", i);
startActivity(intent);
return false;
}
});

关于android - 如何将整个对象传递给 ExpandableListView 中的 onChildClick 中的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43427160/

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