[英]ListView to New Activity then Populate EditText's with SQLite data from Hashmap
I currently have a ListView populated by SQLite and I have implemented an OnItemClickListener to list items. 我目前有一个由SQLite填充的ListView,并且已经实现了一个OnItemClickListener来列出项目。 I want to know how to retrieve values from a Hashmap specific to the item the user clicks in the ListView, then open a new activity and populate the retrieved data into EditTexts.
我想知道如何从Hashmap中检索特定于用户在ListView中单击的项目的值,然后打开一个新活动并将检索到的数据填充到EditTexts中。 Any help would be appreciated!
任何帮助,将不胜感激!
EDIT 编辑
This is what i'm guessing: 这就是我的猜测:
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);
}
Then in my next activity in the onCreate have the following: 然后在onCreate的下一个活动中,有以下内容:
String value = getIntent().getExtras().getString("key");
ET1.setText(value);
Following the HUGE help in the comments from Filipe (Thanks again) the following is the solution to the problem: 在Filipe的评论中提供巨大帮助之后(再次感谢),以下是该问题的解决方案:
In my first activity I have the following in my onItemClick for my ListView: 在我的第一个活动中,我的ListView的onItemClick中包含以下内容:
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);
}
}
In my second activity I then used this piece of code in the onCreate: 在第二个活动中,我在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"));
}
You could retrieve the data for the parent adapterview, something like { parent.getItem(position) } and send that through the intent (instead of retrieving all the data from the controller). 您可以检索父adapterview的数据,类似于{parent.getItem(position)},然后通过意图将其发送(而不是从控制器中检索所有数据)。 On the next activity you'd iterate through the hashmap items and set them to the apropriate EditTexts.
在下一个活动中,您将遍历hashmap项并将它们设置为适当的EditTexts。
Edit: On your public void onItemClick(...)
you should probably use: 编辑:在您的
public void onItemClick(...)
您可能应该使用:
HashMap<String, String> yourHashMap = parent.getItemAtPosition(position); Intent intent = new Intent (parent.getContext(), Record.class); intent.putSerializable("key", yourHashMap);
And on the next activity: 在下一个活动中:
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")); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.