• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Displaying JSON data as a listview

rami186

Lurker
Hey everyone, this is my first thread/post. I think this is the right place to look for some tips and stuff for android programming ahah. So I am completely new at all this java/android programming. My friend gave me the challenge to parse some json data as a listview. I was succesfully able to display the content as a textview however not everything appeared. Only the last few line of text showed up. He told me that by putting it as a listview everything should go well. Now setting up a listview isn't the same thing as setting up a textview. So my question is with all the code that I have how can I display the content as a listview.

[HIGH]DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://ec2-54-213-155-95.us-west-2.compute.amazonaws.com/notices.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}

try {
JSONObject jObject = new JSONObject(result);
JSONArray jsonArray = jObject.getJSONArray("notices");
for(int i = 0; i < jsonArray.length(); i++) {
String arrayString = jsonArray.getString(i);
Log.d("notices", arrayString);


} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}[/HIGH]

thanks everyone !
 
I haven't tried this myself but i'd suggest you to use a 3rd party json parser that makes it easy to serialize and deserialize Java object graphs to and from JSON.
Your way seems ok too but it feels like too much extra work to me
try some libraries like GSon or jsonbeans
 
Put your strings into ArrayList and do this:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, your_array_list);
yourListView.setAdapter(arrayAdapter);
 
Back
Top Bottom