我目前有一个由SQLite填充的ListView,并且我已经实现了一个OnItemClickListener来列出项目。我想知道如何从特定于用户在ListView中单击的项目的Hashmap中检索值,然后打开一个新活动并将检索到的数据填充到EditText中。任何帮助都将不胜感激!
编辑
这就是我猜测的:
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
ArrayList<HashMap<String, String>> scanList = this.controller.getAllRecs();
Intent intent = new Intent (parent.getContext(), Record.class);
intent.putExtra("key", scanList);
}
然后在onCreate的下一个活动中有以下内容:
String value = getIntent().getExtras().getString("key");
ET1.setText(value);
根据Filipe评论中的巨大帮助(再次感谢),以下是问题的解决方案:
在我的第一个活动中,我的onItemClick中有以下内容用于我的ListView:
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
HashMap<String, String> hashmap = (HashMap)parent.getItemAtPosition(position);
Intent intent = new Intent (parent.getContext(), SECONDACTIVITY.class);
intent.putExtra("key", hashmap);
startActivityForResult(intent, 0);
}
}
在我的第二个活动中,我在onCreate:中使用了这段代码
Bundle bundle = getIntent().getExtras();
if(bundle!=null) {
HashMap<String, String> vals = (HashMap)bundle.getSerializable("key");
et1.setText(vals.get("value1"));
et2.setText(vals.get("value2"));
}
您可以检索父adapterwiew的数据,类似于{parent.getItem(position)},并通过intent发送数据(而不是从控制器检索所有数据)。在下一个活动中,您将遍历hashmap项,并将它们设置为适当的EditText。
编辑:在您的public void onItemClick(...)
上,您可能应该使用:
HashMap<String, String> yourHashMap = parent.getItemAtPosition(position);
Intent intent = new Intent (parent.getContext(), Record.class);
intent.putSerializable("key", yourHashMap);
关于下一个活动:
Bundle bundle = getIntent().getExtras();
if(bundle!=null) {
HashMap<String, String> vals = (HashMap)bundle.getSerializable("key");
((TextView)findViewById(R.id.txt1)).setText(vals.get("value1")); ((TextView)findViewById(R.id.txt2)).setText(vals.get("value2")); ((TextView)findViewById(R.id.txt3)).setText(vals.get("value3"));
}