我有一个ListActivity
,它会显示我从互联网上的网络服务中获取的搜索结果列表。我将收到的XML解析为ArrayList<MyObjects>
,然后使用我自己的适配器绑定到ListView
(如MyObjectAdapter
extends ArrayAdapter<MyObject>
)。
那么我希望用户能够点击列表中的一个项目。每个项目都有一个标识符,然后将其放入intent中并发送到新活动(然后根据此活动触发新的Web服务请求,下载其余数据)。但我不知道如何获得所选MyObject
的这一属性。
以下是onListItemClick()
方法:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String myObjectId;
// I want to get this out of the selected MyObject class here
Intent i = new Intent(this, ViewObject.class);
i.putExtra("identifier_key", myObjectId);
startActivityForResult(i, ACTIVITY_VIEW);
}
答案 0 :(得分:3)
如果您使用的是ArrayAdapter,则必须使用数组初始化该适配器,对吧?因此,您可以做的就是使用从postition
获得的onListItemClick
并从原始数组中获取对象。
例如:
// somewhere you have this
ArrayList<MyObjects> theItemsYouUsedToInitializeTheArrayAdapter;
// and inside onListItemClick....
String myObjectId = theItemsYouUsedToInitializeTheArrayAdapter.get(position).getObjectId();
答案 1 :(得分:0)
试试这个getIntent().getExtras().getString(key);