我对listview项目有疑问。所有列表视图项在单击列表视图时显示所有列表视图项的哈希映射的最后一个值。如何解决这个问题。我已经完成了这样的编码。
代码:
protected Void doInBackground(Void... params) {
HashMap<String, String> contact = new HashMap<String, String>();
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(
url);
HttpResponse response = httpclient.execute(httpget);
String jsonResp = EntityUtils.toString(response
.getEntity());
Log.d("HTTP", "Rsponse : " + jsonResp);
if (jsonResp != null) {
JSONObject jsonObject1 = new JSONObject(jsonResp);
JSONArray jsonArray = jsonObject1.getJSONArray("values");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
firstname = jsonObject2.getString(firstname);
lastname = jsonObject2.getString(lastname);
headline = jsonObject2.getString(headline);
pictureUrl = jsonObject2.getString(pictureUrl);
id = jsonObject2.getString(id);
JSONObject jsonObject3 = jsonObject2.getJSONObject("siteStandardProfileRequest");
url = jsonObject3.getString("url");
}
Log.d("HTTP", "firstname : " + firstname + "\n" + "lastName :"
+ lastname + "\n" + "headline : " + headline + "\n"
+ "pictureUrl :" + pictureUrl + "\n" + "id :"
+ id + "\n" + "Url :" + url);
contact = new HashMap<String, String>();
contact.put(TAG_ID, id);
contact.put(TAG_FNAME, firstname);
contact.put(TAG_LNAME, lastname);
contact.put(TAG_HLINE, headline);
contact.put(TAG_PURL, pictureUrl);
contact.put(TAG_URL, url);
contactList.add(contact);
}
} else {
Log.e("HTTP", "Couldn't get any data from the url");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
SimpleAdapter adapter = new SimpleAdapter(
HomeActivity.this, contactList,
R.layout.list_item, new String[]{TAG_FNAME}, new int[]{R.id.textView1});
setListAdapter(adapter);
adapter.notifyDataSetChanged();
Collections.sort(contactList, new Comparator<Map<String, String>>() {
public int compare(final Map<String, String> o1, final Map<String, String> o2) {
return o1.get("firstname").compareTo(o2.get("firstname"));
}
});
lv.setTextFilterEnabled(true);
lv.setFastScrollEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position, long ids) {
Intent i = new Intent(getApplicationContext(),
SingleListActivity.class);
i.putExtra("id", id);
i.putExtra("firstname", firstname);
i.putExtra("lastname", lastname);
i.putExtra("headline", headline);
i.putExtra("pictureUrl", pictureUrl);
i.putExtra("url", url);
Log.v("Pass", "id :" + id + "\n" + "firstname :" + firstname + "\n"
+ "lastname :" + lastname + "\n" + "headline :" + headline
+ "\n" + "pictureUrl :" + pictureUrl + "\n"
+ "siteStandardProfileRequest" + url);
startActivity(i);
}
});
}
答案 0 :(得分:1)
我使用了相同的ListView,我可以帮助你。
1)我会删除第一行( HashMap contact = new HashMap(); )并在下面的循环中添加每个记录所需的新HashMap。因此,替换以下行:
<强> contact = new HashMap<String, String>();
强>
使用:
HashMap<String, String> contact = new HashMap<String, String>();
此外,我会将以下内容用于
private static final String TAG_FName = "FirstName";
然后在JSOn Object,
String fname = c.getString(TAG_FName)
后来:
ListAdapter adapter = new SimpleAdapter(Activity.this, contactList, R.layout.list_item, new String[] { TAG_FName, },new int[] { R.id.TEXTVIEW});