Android ListView: looping through json array to list -


been working on few hours now, not sure how should going sorting through test array. i've tried few different options have seen posted 1 simplest implement project. suggestions or ideas might me on hump?

json array php

{     "questions": {         "0001": {             "title": "what stackoverflow",             "answer": "c",             "user": "testuser",             "date": "0000-00-00",             "used": "0"         },         "0002": {             "title": "what stackoverflow",             "answer": "c",             "user": "testuserb",             "date": "0000-00-00",             "used": "1"         },         "0003": {             "title": "what stackoverflow",             "answer": "c",             "user": "testuserc",             "date": "0000-00-00",             "used": "0"         }     },     "count-start": 1,     "count-end": 3 } 

used: 0 = not been used 1 = user has used. use determine whether or not show user question , answer.

helper class

public jsonobject query(){         httpclient client = new defaulthttpclient();         httpconnectionparams.setconnectiontimeout(client.getparams(), 10000);         httpresponse response;         jsonobject json = new jsonobject();         httppost post = new httppost(query_link);         post.setheader("json", json.tostring());         stringentity se;         try {             se = new stringentity(json.tostring());             se.setcontentencoding(new basicheader(http.content_type,"application/json"));             post.setentity(se);             response = client.execute(post);             if (response != null) {                 inputstream in = response.getentity().getcontent();                 = convertstreamtostring(in);                 jsonobject check = new jsonobject(a);                    return check;             }         } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         } catch (jsonexception e) {             e.printstacktrace();         }          return json;     } 

main activity

private arraylist<listhelper> getsearchresults(){         jsonobject getjson = new helper().query();         jsonarray jarrayobject = new jsonarray();         jarrayobject.put(getjson);         int end = 0,start = 0;         string title, answer, user, used;         listhelper sr = new listhelper();        arraylist<listhelper> results = new arraylist<listhelper>();      jsonobject offerobject = null;         try {             offerobject = getjson.getjsonobject("questions");             start = offerobject.getint("count-start");             end = offerobject.getint("count-end");         } catch (jsonexception e) {             // todo auto-generated catch block             e.printstacktrace();         }        for(int = start; < end; = a++) {          try {               jsonobject businessobject = offerobject.getjsonobject("id");             // i'm stuck, how go selecting each id              title = businessobject.getstring("title");             answer = businessobject.getstring("answer");             user = businessobject.getstring("user");             used = businessobject.getstring("used");              sr = new listhelper();              sr.settitle(title);              sr.setuser(user);              sr.setused(used);              results.add(sr);         } catch (jsonexception e1) {             e1.printstacktrace();         }       }      return results;     } } 

you can below purpose: define new decimalformat outside for loop

decimalformat myformatter = new decimalformat("0000");  

then inside for loop form id string using formatter.

string id = myformatter.format(a); 

and use id fetching jsonobject

// i'm stuck, how go selecting each id jsonobject businessobject = offerobject.getjsonobject(id); 

the above work if ids @ max 4 digits. can make format dynamic using "count-end" value instead of using hard coded "0000".

all trouble have been avoided if "questions" object jsonarray.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -