Java ListView到新活动,然后填充EditText';使用Hashmap中的SQLite数据
我目前有一个由SQLite填充的ListView,并且我已经实现了一个用于列出项目的ListView监听器。我想知道如何从特定于用户在ListView中单击的项目的Hashmap中检索值,然后打开一个新活动并将检索到的数据填充到EditText中。任何帮助都将不胜感激 编辑 我猜是这样的:Java ListView到新活动,然后填充EditText';使用Hashmap中的SQLite数据,java,android,sqlite,listview,hashmap,Java,Android,Sqlite,Listview,Hashmap,我目前有一个由SQLite填充的ListView,并且我已经实现了一个用于列出项目的ListView监听器。我想知道如何从特定于用户在ListView中单击的项目的Hashmap中检索值,然后打开一个新活动并将检索到的数据填充到EditText中。任何帮助都将不胜感激 编辑 我猜是这样的: public void onItemClick(AdapterView<?> parent, View view, int position, long i
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);
}
您可以检索父adapterview的数据,类似于{parent.getItem(position)},并通过intent发送该数据(而不是从控制器检索所有数据)。在下一个活动中,您将遍历hashmap项,并将它们设置为适当的EditText 编辑: 在您的
公共页面上,单击(…)
您可能应该使用:
HashMap yourHashMap=parent.getItemAtPosition(位置);
Intent Intent=新的Intent(parent.getContext(),Record.class);
intent.putSerializable(“key”,yourHashMap)代码>
关于下一个活动:
Bundle Bundle=getIntent().getExtras();
if(bundle!=null){
HashMap 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”);
}
在菲利佩评论中提供了大量帮助之后(再次感谢),以下是问题的解决方案:
在我的第一个活动中,我在我的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);
}
}
public void onItemClick(AdapterView父视图、视图、int位置、,
长id){
HashMap HashMap=(HashMap)parent.getItemAtPosition(position);
Intent Intent=新的Intent(parent.getContext(),SECONDACTIVITY.class);
intent.putExtra(“键”,hashmap);
startActivityForResult(意向,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"));
}
Bundle Bundle=getIntent().getExtras();
if(bundle!=null){
HashMap vals=(HashMap)bundle.getSerializable(“key”);
et1.setText(vals.get(“value1”);
et2.setText(vals.get(“value2”);
}
如果您有一些示例代码要添加,那么给您一个具体的答案可能会更容易。只需编辑您的问题并将其扔到那里即可。:)嘿,巴雷特。这就是我猜的,我知道还有一点距离,但我希望你能理解,哈哈,我不熟悉迭代hashmap并将其放入编辑文本中。你能给我举个例子吗?谢谢你,教授,我刚刚意识到你很可能不需要反复阅读它们。您只需通过如下键访问它们:HashMap vals=getVals();((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”)
@dotexecutableBut出于好奇,如果您想遍历可以使用的值:for(Map.Entry条目:vals.entrySet()){final String key=Entry.getKey();final String value=Entry.getValue();}
@dotexecutableSo要使用您的第一个回复,我将使用以下内容:;Intent=getIntent();在我的第二页,那么你的以下代码?对不起,我对这方面还不太熟悉,谢谢你的帮助@Filipe Silvia你的AdapterView里面是什么样子的?我假设您使用自定义AdapterView。如果存储在其中的数据是ArrayList
,则使用HashMap myHashMap=parent.getItemAtPosition(position)获得HashMap
然后通过intent发送该hasmap。检查编辑后的答案
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"));
}