我有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