Ich habe Button, wenn ich Speicher ObjectArray
in sharedPreference
geklickt habe, jetzt möchte ich eine andere Schaltfläche entfernen ObjectArray
.Entfernen Sie JSONObject von Sharedreference
wie man einen Eintrag entfernt? Ich schaffe es kann das ganze Datum von sharedPreference zu löschen, aber es ist nicht, was ich will ...
Hier ist mein Code zu speichern:
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();
}
}
Dank im Voraus
Was ist Ihr Problem? Haben Sie einen Fehler gemacht? –
Ich möchte einen Eintrag aus sharedPreference entfernen ... – Riz
Ich kann nicht finden, die SharedPreferences in Ihrem Code verwenden – Pooya