I've been trying to store the parsed Json data into Listview but it's not working.
I retreived some data(which is in the form of table) from a server and then i parsed it into Json object. The whole code worked well. It displays the retreived data when i use 'Toast' in my android activity. Please help me to store this parsed data in 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;
}
There is no error but the activity doesn't show the values in the Listview but values can be seen using 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"/>
First thing to mention:
Check Again that Your HashMap gets the correct values or not,
Log your values in your for loop like Log.i("item",jObject.getString("time"));
and second thing make your own custom list Adapter
class,
USING THIS EXAMPLE SAME AS YOURS
It seems the SimpleAdapter you're using should be case sensitive? You're passing "Time", "Latitude", and "Longitude" as your columns but your JSON object contains "time", "latitude", and "longitude".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With