我有2项活动。Cant从列表视图中随机选择数据
第一个活动是将JSON数据从服务器分析到ListView
的活动,第二个活动是显示ListView
中选定值的数据的活动。
这是我的第一个活动的onClick
方法:
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, userList,
R.layout.listitemviewall,
new String[] { "namaresto", "alamatresto"},
new int[] {R.id.name, R.id.address});
setListAdapter(adapter);
/**
* select resto
*/
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/**
* move to the next activity
* */
String namanya = ((TextView) findViewById(R.id.name)).getText().toString();
String alamatnya = ((TextView) findViewById(R.id.address)).getText().toString();
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), InfoTab.class);
//Sending data to another Activity
nextScreen.putExtra("name", namanya);
nextScreen.putExtra("address", alamatnya);
// starting new activity
startActivity(nextScreen);
}
});
这里是第二个活动:
TextView txtname = (TextView) findViewById(R.id.textTEST1);
TextView txtaddress = (TextView) findViewById(R.id.TextTEST2);
//displaying data from previous activity
Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
String address = i.getStringExtra("address");
// Displaying Received data
txtname.setText(name);
txtaddress.setText(address);
结果,第二活动始终显示的第一条记录!即使我点击了ListView
中的第二个或其他记录。
如何让我的代码显示在ListView
中选择的正确记录?
非常感谢你... 这对我来说很合适! – 2012-03-29 14:09:28
快乐吧:) – Th0rndike 2012-03-29 14:15:14