0

I am using a bundled json file in my application. I want to read and write data into the json file. I have a listview which displays the currently available data in json and an edittext into which user enters data. That data has to be written into the json. How to do this?

This is my code...

 public class MainActivity extends Activity { String myjsonstring; ArrayList<Data> web = new ArrayList<Data>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); JsonParser(); } private void JsonParser() { // Reading text file from assets folder StringBuffer sb = new StringBuffer(); BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(getAssets().open( "single.json"))); String temp; while ((temp = br.readLine()) != null) sb.append(temp); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); // stop reading } catch (IOException e) { e.printStackTrace(); } } myjsonstring = sb.toString(); // Try to parse JSON try { JSONObject jsonObjMain = new JSONObject(myjsonstring); JSONArray jsonArray = jsonObjMain.getJSONArray("message"); for (int i=0; i<jsonArray.length(); i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); // Getting data from individual JSONObject Data data = new Data(jsonObj.getString("name") , jsonObj.getString("msg")); web.add(data); } final customtest1 adapter = new customtest1(MainActivity.this,R.layout.list_single,web); final ListView list = (ListView)findViewById(R.id.list); list.setAdapter(adapter); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show(); adapter.notifyDataSetChanged(); } }); //Send activity...... final EditText et = (EditText)findViewById(R.id.EditText1); final Button imb=(Button)findViewById(R.id.btn_send); imb.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String str = et.getText().toString(); Data store = new Data(et.getText().toString()); web.add(store); Toast.makeText(MainActivity.this, "You entered...."+store, Toast.LENGTH_SHORT).show(); et.setText(" "); scrollMyListViewToBottom(); adapter.notifyDataSetChanged(); } //Screen auto scrollup.... private void scrollMyListViewToBottom() { list.post(new Runnable() { @Override public void run() { // Select the last row so it will scroll into view... list.setSelection(adapter.getCount() - 1); } }); }}); } catch (JSONException e) { e.printStackTrace(); } } } 
1
  • You cannot edit files added as resources, make a copy of it in your application data folder Commented Apr 7, 2014 at 7:05

2 Answers 2

0

In my opinion,you could parse the jsonarray from the exist file,then add the new data to the old jasonarray,then write the new data to the exist file.

Sign up to request clarification or add additional context in comments.

Comments

0

you cannot edit file in asset folder at runtime .load file into memory and then try this method to write to internal memory :

private void writeToFile(String jsonData) { try { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("single.json", Context.MODE_PRIVATE)); outputStreamWriter.write(jsonData); outputStreamWriter.close(); } catch (IOException e) { Log.e("Exception", "File write failed: " + e.toString()); } } 

5 Comments

Which data is being written into this file ?How to pass the data ?
this is to write on internal memory. you cn not change file in asset folder at runtime.read here stackoverflow.com/questions/21637576/…
oh ok.so when i am using a json url in real time,i can do that but not to the json file in assets folder right ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.