2

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

Riz
  • 75
  • 9

2 Answers2

0

To remove a specific saved preferences then use

SharedPreferences.Editor editor = settings.edit();
editor.remove("tag_to_delete");
editor.commit();

(or)

Using the remove method is probably the proper way.

mPrefs = PlaniActivity.this.getPreferences(MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
prefsEditor.remove("list");

You also only need one editor, so just keep removing items

prefsEditor.remove("platamatBeansArrayList");

And you only need to apply or commit once at the very end because all events are queued up.

prefsEditor.apply();
  • Thanks AndroidEnthusiastic for your replay..i can't access to my work right now i will test it later i will let you know – Riz Apr 04 '16 at 08:15
0

I did not see anything about Shared preference in your code. You have saved your json string to a file.. although you can put this json string to a shared preference like this way

first try to get a sharedpreference object

SharedPreferences mySharedPreferences=getSharedPreferences("myStore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("jString1", yourJsonString1);
editor.putString("jString2",yourJsonString2);
editor.commit();

Now time for removing something from shared preference. try to get sharedpreference object name 'myStore' that you have create earlier

SharedPreferences mySharedPreferences=getSharedPreferences("myStore", Context.MODE_PRIVATE);
if(sharedPreferences != null) {
   SharedPreferences.Editor editor = mySharedPreferences.edit();
   editor.remove("jString1");
   editor.commit();
}

above is the normal procedure.. but i found something tricky in your code. Your jsonEntry is a jsonString and it contains JsonArray (completeArray) and this array contains another jsonObject 'finalEntry' and finally this 'finalEntry' has a 'entry' jsonObject. may be you want to remove this entry object from sharedPreference. oky lets do that i assume that you have stored jsonEntry in with jsonEntry1 key sp. try to retrieve that value.

String jsonEntry =mySharedPreferences.getString("jsonEntry1",null);
if(jsonEntry != null){
    JSONArray completeArray = new JSONArray(jsonEntry);
    for (int i=0;i<completeArray .length();i++){
     JSONObject finalEntry=(JSONObject) completeArray .get(i);
       if(finalEntry.has("entry")){
        finalEntry.remove("entry");// for remove only one entry object you need to add your own logic 
       }  
    }
}

after finish the loop your completeArray will be 'entry' free arrayObject and you can put it again to sharedpreference ..

Hope it works. ( may be you need to customize this logic )

Tanim reja
  • 2,120
  • 1
  • 16
  • 23
  • Thanks Tanim reja for your replay, you are right I have saved my json string to a file. but unfortunately i couldn't successful to remove entry logView.setOnLongClickListener(new View.OnLongClickListener() .. i'm trying to save it on a sharedprefrance. probably find solution easier. i'm not sure am i wrong or right? – Riz Apr 04 '16 at 11:02
  • if you have any idea to remove an entry from json from file, i will appreciate. i will go back to file. save json on shared preference too complicated – Riz Apr 04 '16 at 11:22
  • yah you can read and write again and again to a file...can you try to read that json after write it to a file.. then it would be easy. but you are wrong about SharedPreference... this is not complicated ... its too easy then file system. you need to try it..( i think your json structure is much complicated you can re arrange the logic if possible) – Tanim reja Apr 04 '16 at 12:28