android - How to edit an item in a json file? -


i have structure generated application , read in listview:

[{   "phone": "202020",   "name": "jhon",   "id": 10,   "age": 20 }, {   "phone": "303030",   "name": "rose",   "id": 11,   "age": 22 }] 

when select item in listview, open screen form passing values ​​of clicked item.

lv.setonitemclicklistener(new onitemclicklistener() {      @override     public void onitemclick(adapterview<?> parent, view view,             int position, long id) {         // getting values selected listitem         string name = ((textview) view.findviewbyid(r.id.name)).gettext().tostring();         string age = ((textview) view.findviewbyid(r.id.age)).gettext().tostring();         string phone = ((textview) view.findviewbyid(r.id.phone)).gettext().tostring();          // starting new intent         intent in = new intent(getapplicationcontext(), singlemenuitemactivity.class);         in.putextra(tag_name, name);         in.putextra(tag_agge, age);         in.putextra(tag_phone, phone);         startactivity(in);     } }); 

this screen opens when click on item form put values ​​passed previous screen fields.

my question is: when click save in form, have new values ​​and update json file. how this?

ex: want change record id 22, user rose.

add more information:

i use gson generate items.

code generate:

btnsalvar.setonclicklistener( new view.onclicklistener() { public void onclick(view v) {     gson = new gsonbuilder().setprettyprinting().create();     final file file = new file(environment.getexternalstoragedirectory() + "/download/ibbca/auditar.json");      // check if file exists before before maybe created     if (file.exists())         fileexists = true;      try{          // create file or access file         raf = new randomaccessfile(file, "rw");          if (fileexists) // start new file array             raf.seek(file.length() - 1);         else { // start writing inside bracket             raf.writebytes("[");             raf.seek(file.length());         }          usertestjson obj1 = new usertestjson();         obj1.setid(10);         obj1.setname("jhon");         obj1.setage(20);         obj1.setphone("202020");          tojson(obj1);          // end file         raf.writebytes("]");         raf.close();     }catch(filenotfoundexception f){         f.printstacktrace();     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     }  } });  

and class usertestjson, created , seters each variable.

the simplest way is, go json object , set desired value key.

jsonarray arr = new jsonarray(str); for(int = 0; < arr.length(); i++){      jsonobject jsonobj = (jsonobject)arr.get(i); // josn object     if(jsonobj.getstring("name").equals("rose")){ // compare key-value         ((jsonobject)arr.get(i)).put("id", 22); // put new value key     }     textview.settext(arr.tostring());// display , verify json updated value } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -