Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to edit an item in a json file?
    primarykey
    data
    text
    <p>I have this structure that is generated by my application and I read in a listView:</p> <pre><code>[{ "phone": "202020", "name": "Jhon", "id": 10, "age": 20 }, { "phone": "303030", "name": "Rose", "id": 11, "age": 22 }] </code></pre> <p>When I select an item in the listview, I open a screen form passing the values ​​of the clicked item.</p> <pre><code>lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { // getting values from 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); } }); </code></pre> <p>This screen opens when you click on the item is a form where I put the values ​​passed from the previous screen fields.</p> <p>My question is: When you click save in this form, I have to get the new values ​​and update the json file. How to do this?</p> <p>Ex: I want to change the record ID 22, which is the user Rose.</p> <p>ADD MORE INFORMATION:</p> <p>I already use the Gson to generate items.</p> <p>Code to generate:</p> <pre><code>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"); // to check if file exists before before it maybe will be created if (file.exists()) fileExists = true; try{ // create file or get access to file raf = new RandomAccessFile(file, "rw"); if (fileExists) // start new file as an array raf.seek(file.length() - 1); else { // start writing inside the 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(); } } }); </code></pre> <p>And the Class UserTestJson, i created with get and seters for each variable.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload