我已經完成了解析json並將其獲取到ListView,但我有一個問題當點擊ListView中的項目只顯示最後解析的Json數據,當我點擊時按位置顯示正確數據的項目:(onItemClick)從列表視圖顯示Json數據 - Android
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
final String fname = c.getString("fname");
final String lname = c.getString("lname");
String username = c.getString("username");
//String user_id = c.getString("user_id");
//String gender = c.getString("gender");
HashMap<String, String> contact = new HashMap<>();
contact.put("fname", fname);
contact.put("lname", lname);
contact.put("username", username);
users.add(contact);
ListAdapter adapter = new SimpleAdapter(FindPeopleActivity.this, users,
R.layout.list_item, new String[]{"fname", "lname", "username"},
new int[]{R.id.fname, R.id.lname, R.id.username});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent , View view , int position , long id){
Intent i = new Intent(FindPeopleActivity.this,UserProfileActivity.class);
Toast.makeText(getApplicationContext(),fname,Toast.LENGTH_SHORT).show();
}
});
}
你的listview被填充?它看起來像在for循環中設置適配器和onClickListener。 –