Android Display Json Object To Listview
This is the code I'm working on: private final static String SERVICE_URI = 'http://restwebservice.com/test/Service.svc'; StringEntity entity; String var; @Override protected
Solution 1:
One of the most popular tutirials on list views which may help you:
Steps to follow after parsing your json:
1. Map your json objects to pojo.
2. Store your pojo in an array list if many are there.
3.Create a list viewwith a custom adapter.
4.update your listview with answer from the pojo's that you have mapped with
notifyDatasetChanged
You can use jackson library to parse json with one line of code.
//1. Convert Java object to JSON formatObjectMappermapper=newObjectMapper();
mapper.writeValue(newFile("c:\\user.json"), user);
//2. Convert JSON to Java objectObjectMappermapper=newObjectMapper();
Useruser= mapper.readValue(newFile("c:\\user.json"), User.class);
(follow this link for more on object mapping tutorial)
Solution 2:
this is what i did to generate listview from my json response, basically i wrote values from my jon response to adapter and set it to my list view
try{
jArray = newJSONArray(result);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
Log.i("log_tag","dealid: "+json_data.getString("deal_id")+
", hotel name: "+json_data.getString("hotel_name")+
", location: "+json_data.getString("location")+
", website: "+json_data.getString("website")
);
}
json_data=newJSONObject();
String[] data=newString[jArray.length()];
planetList = newArrayList<String>();
for(int i=0;i<jArray.length();i++)
{
json_data= jArray.getJSONObject(i);
data[i]=json_data.getString("deal_id");
Log.i("log_tag", "string "+data[i]);
planetList.addAll( Arrays.asList("Deal "+ (i+1)));
listAdapter = newArrayAdapter<String>(this, R.layout.listrow, planetList);
runOnUiThread(newRunnable() {
publicvoidrun() {
list.setAdapter(listAdapter);
}
});
}
list.setOnItemClickListener(newOnItemClickListener() { //where to put this piece of code???publicvoidonItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
Intent intent = newIntent(context,Finaldeal.class);
intent.putExtra("deal", json_data.toString());
startActivity(intent);
}
});
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
you can also refer to question i asked here
Post a Comment for "Android Display Json Object To Listview"