我一直在尝试将解析的 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 看到值。! 使用 Toast 输出活动
这是我的 geo_ponts_list.xml 文件
<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"/>