Wednesday, April 10, 2013

[android help] JSON String to ListView not happening


I have a JSON String, I am trying to parse and then into ListView, my code does not throw any errors but it is not working either, I am unable to determine where I might be going wrong. Following is my code:



protected void onPostExecute(String result) {
super.onPostExecute(result);
str = result;
Toast.makeText(getApplicationContext(),""+str, Toast.LENGTH_LONG).show(); // Able to toast the JSON string (str) here.
mylist = new ArrayList>();
map = new HashMap();
try {
JSONArray jArray = new JSONArray(str);
for (int i = 0; i < jArray.length(); i++) {

JSONObject jObject = jArray.getJSONObject(i);


j_id = jObject.getString(ID);

j_make = jObject.getString(MAKE);

j_model = jObject.getString(MODEL);

j_version = jObject.getString(VERSION);
j_price = jObject.getString(PRICE);
j_reg_plc = jObject.getString(PLACE_REG);


Toast.makeText(getApplicationContext(),"After assign", Toast.LENGTH_LONG).show();



data = "Make: "+ j_make + "\nModel: " + j_model + "\nVersion: " + j_version + "\nPrice: " + j_price
+ "\nCity: " + j_reg_plc;



map.put("car", data);

mylist.add(map);

}
} catch (JSONException e) {
e.printStackTrace();
}




// list = (ListView) findViewById(R.id.listView1);
Toast.makeText(getApplicationContext(),""+mylist, Toast.LENGTH_LONG).show();
String[] from = new String[] { "car"};
int[] to = new int[] { R.id.text1 };
ListAdapter adapt = new SimpleAdapter(view_cars.this, mylist,R.layout.text_adaptr, from, to);

list.setAdapter(adapt);


}


It seems that my for loop is ubruptly exiting, It is not getting executed twice as the length of the array I am recieving from network is two. I have attached the screen shot of the JSON Array I am getting enter image description here


Please check the code for comments, for detailed information.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...