我一直在尝试将已解析的Json数据存储到Listview中,但它无效。
我从服务器中检索了一些数据(以表格的形式),然后我将其解析为Json对象。 整个代码运作良好。当我在Android活动中使用“Toast”时,它会显示已检索的数据。 请帮我将这个解析后的数据存储在Listview中。
public void clickbutton(View v) {
try {
// Log.i(getClass().getSimpleName(), "send task - start");
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
//
HttpParams p = new BasicHttpParams();
// p.setParameter("name", pvo.getName());
p.setParameter("user", "1");
// Instantiate an HttpClient
HttpClient httpclient = new DefaultHttpClient(p);
String url = "http://bhavit.xtreemhost.com/webservice1.php?user=1&format=json";
HttpPost httppost = new HttpPost(url);
// Instantiate a GET HTTP method
try {
Log.i(getClass().getSimpleName(), "send task - start");
//
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
7);
nameValuePairs.add(new BasicNameValuePair("details", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost,
responseHandler);
// Parse
JSONObject json = new JSONObject(responseBody);
JSONArray jArray = json.getJSONArray("posts");
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jArray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jArray.getJSONObject(i);
String s = e.getString("post");
JSONObject jObject = new JSONObject(s);
map.put("time", jObject.getString("time"));
map.put("latitude", jObject.getString("latitude"));
map.put("longitude", jObject.getString("longitude"));
mylist.add(map);
}
Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();
**String[] columns = new String[] { "Time", "Latitude", "Longitude" };
int[] renderTo = new int[] { R.id.time, R.id.latitude, R.id.longitude };
ListAdapter listAdapter = new SimpleAdapter(this, mylist, R.layout.geo_ponts_list, columns, renderTo);
setListAdapter(listAdapter);**
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Log.i(getClass().getSimpleName(), "send task - end");
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
private void setListAdapter(ListAdapter listAdapter) {
// TODO Auto-generated method stub
}
public class Data {
// private List<User> users;
public List<details> users;
// +getters/setters
}
static class details {
String latitude;
String longitude;
String time;
public String Longitude() {
return longitude;
}
public String Latitude() {
return latitude;
}
public String Time() {
return time;
}
public void setUserName(String value) {
longitude = value;
}
public void setidusers(String value) {
latitude = value;
}
没有错误,但活动未显示Listview中的值,但可以使用Toast查看值。 Output Of Activity using Toast
<TextView android:id="@+id/time"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/latitude"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/longitude"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
答案 0 :(得分:4)
首先要提到的是:
再次检查您的HashMap是否获得正确的值,
将您的值记录在for循环中,如Log.i("item",jObject.getString("time"));
第二件事就是制作你自己的custom list Adapter
课程,
答案 1 :(得分:2)
您使用的SimpleAdapter似乎应区分大小写?您将“时间”,“纬度”和“经度”作为列传递,但您的JSON对象包含“时间”,“纬度”和“经度”。
答案 2 :(得分:2)
我给你的代码一试,正如Quenton所说,这是案例的问题,使得listview没有显示任何内容。
**String[] columns = new String[] { "Time", "Latitude", "Longitude" };
int[] renderTo = new int[] { R.id.time, R.id.latitude, R.id.longitude };
ListAdapter listAdapter = new SimpleAdapter(this, mylist, R.layout.geo_ponts_list, columns, renderTo);
setListAdapter(listAdapter);**
您应修改columns变量以使值小写。它对我有用。
String[] columns = new String[] {"time", "latitude', 'longitude'};