I have button when I clicked store ObjectArray
into sharedPreference
, now I want another button to remove ObjectArray
.
how to remove one entry? I can manage to clear all date from sharedPreference but its not what i want...
Here's my code to Store:
private void logDateTime() {
TextView name = (TextView) findViewById(R.id.tv_tel);
String m1 = name.getText().toString();
mTvName = (TextView) findViewById(R.id.tv_name);
Intent i = new Intent(CountryActivity1.this, LogActivity.class);
String m2 = mTvName.getText().toString();
startActivity(i);
// Variables
String date = DateFormat.getDateInstance().format(new Date());
String time = DateFormat.getTimeInstance().format(new Date());
String desc = m2;
String desc1 = m1;
JSONArray completeArray = new JSONArray();
try {
// Open the JSON file and initialize a string builder
FileInputStream in = openFileInput(FILENAME);
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
// Read the existing content in the JSON file and add it to the
// string builder
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
if (sb.toString() != "") {
JSONArray temp_arr = new JSONArray(sb.toString());
completeArray = temp_arr;
}
} catch (IOException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
// Initialize the JSON object for the new entry
JSONObject entry = new JSONObject();
// Initialize the JSON object that will contain the entry object
JSONObject finalEntry = new JSONObject();
try {
// Add the time and date to the entry object
entry.put("date", date);
entry.put("time", time);
entry.put("description", desc);
entry.put("description1", desc1);
// Add the entry object to a new object called "entry"
finalEntry.put("entry", entry);
completeArray.put(finalEntry);
// Convert the complete array in to a string
String jsonEntry = completeArray.toString();
// Write complete array to the file
FileOutputStream fos = openFileOutput(FILENAME,
Context.MODE_PRIVATE);
fos.write(jsonEntry.getBytes());
fos.close();
// Notify that an entry has been created
Toast toast = Toast.makeText(getApplicationContext(), "Saved",
Toast.LENGTH_LONG);
toast.show();
}
catch (JSONException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
thanks in advance