android - How can I populate a ListView(TwoWayView) with json? -


i have json saved @ apps data folder want use populate listview with.

the json structure basic:

{     "somelist": [         {             "first": "first item"             "second": "second item"         },         {             "first": "third item",             "second": "fourth item"         },         {             "first": "fifth item",             "second": "sixth item"         }     ] } 

in set hardcoded text with

viewholder.first.settext("first item"); viewholder.second.settext("second item"); 

is there way have retrieve data stored json file?

the short answer yes there way.

here steps:

  1. load file directory

    inputstream = getresources().openrawresource(r.raw.json_file); writer writer = new stringwriter(); char[] buffer = new char[1024]; try {     reader reader = new bufferedreader(new inputstreamreader(is, "utf-8"));     int n;     while ((n = reader.read(buffer)) != -1) {        writer.write(buffer, 0, n);    } } {     is.close(); } string jsonstring = writer.tostring(); 
  2. convert json can go through

    jsonobject jsonobject = new jsonobject(jsonstring); jsonarray jsonarray = jsonobject.getjsonarray("somelist"); 
  3. now enumerate through items in list , add them viewholder

    for(int i=0;i<jsonarray.length();i++){      jsonobject curr = jsonarray.getjsonobject(i);      viewholder.first.settext(curr.getstring("first"))      viewholder.second.settext(curr.getstring("second"))      //...now add each viewholder object , you're done } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -