Reputation: 92
I have this structure that is generated by my application and I read in a listView:
[{
"phone": "202020",
"name": "Jhon",
"id": 10,
"age": 20
},
{
"phone": "303030",
"name": "Rose",
"id": 11,
"age": 22
}]
When I select an item in the listview, I open a screen form passing the values of the clicked item.
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> 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);
}
});
This screen opens when you click on the item is a form where I put the values passed from the previous screen fields.
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?
Ex: I want to change the record ID 22, which is the user Rose.
ADD MORE INFORMATION:
I already use the Gson to generate items.
Code to 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");
// 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();
}
}
});
And the Class UserTestJson, i created with get and seters for each variable.
Upvotes: 1
Views: 15510
Reputation: 474
The Simplest way is, Just go to that json Object and set the desired value for the key.
JSONArray arr = new JSONArray(str);
for(int i = 0; i < arr.length(); i++){
JSONObject jsonObj = (JSONObject)arr.get(i); // get the josn object
if(jsonObj.getString("name").equals("Rose")){ // compare for the key-value
((JSONObject)arr.get(i)).put("id", 22); // put the new value for the key
}
textview.setText(arr.toString());// display and verify your Json with updated value
}
Upvotes: 5
Reputation: 781
I cant find a direct way to do so.
But you can use this function to solve the problem first. See if there is other solution
private JSONObject setJSONVal(JSONObject jsonObject, String index, String value) throws JSONException{
String jsonString = jsonObject.toString().trim();
jsonString = jsonString.replace("\"" + index + "\":\"" + jsonObject.getString(index) + "\"", "\"" + index + "\":\"" + value + "\"");
return new JSONObject(jsonString);
}
Upvotes: 0
Reputation: 11537
It's perhaps a good time to switch to an ArrayAdapter. I recommend to transfer the JSON into a custom model bean first:
class Person {
long id;
String phone, name, age;
}
Then you can use an JSON parser library like gson to parse the array into a List<Person>
and use this array to drive your list. (See Gson help with parse array - works without array but won't with array) for an example on the parsing.
Finally, when you are ready to write back the data, simply re-generate the JSON from the array. This question got an example for that: Trouble with Gson serializing an ArrayList of POJO's
Pros:
Cons:
Upvotes: 1