Can't remove SharedPreferences

2019-09-06 20:32发布

问题:

First, I was using Shared preferences for my application for sending data from one activity to another, when listview is clicked in first activity, but I have a trouble when I click another list, the detail from that other list is the same as the first.
When I try to use intent, it's still the same. I have used:

settings.edit().clear().commit();

and

settings.edit().remove().commit();

but it doesn't work. is there any wrong? this is my first activity :

lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
//              SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
//              settings.edit().clear().commit();
//              settings.edit().remove("nama").commit();
//              settings.edit().remove("alamat").commit();
//              settings.edit().remove("ket").commit();
//              settings.edit().remove("img_id").commit();
//              settings.edit().remove("telp").commit();
//              settings.edit().remove("begdate").commit();
//              settings.edit().remove("enddate").commit();

                Intent detail= new Intent (TerbaruSimasCard.this, TerbaruDetail.class);
                detail.putExtra("nama", nama);
                detail.putExtra("alamat",alamat);
                detail.putExtra("ket", ket);
                detail.putExtra("telp",telp);
                detail.putExtra("begdate", begdate);
                detail.putExtra("enddate",enddate);
                detail.putExtra("img_id", img_id);

//              SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
//              settings.edit().clear().commit();
//              startActivity (detail);
//              Intent detail= new Intent (TerbaruSimasCard.this, TerbaruDetail.class);
                startActivity (detail);

//              SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
//              SharedPreferences.Editor editor = settings.edit();
//              editor.putString("nama", nama);
//              editor.putString("alamat",alamat);
//              editor.putString("ket", ket);
//              editor.putString("telp",telp);
//              editor.putString("begdate", begdate);
//              editor.putString("enddate",enddate);
//              editor.putString("img_id", img_id);
//              editor.commit();
//              System.out.println(nama+"nama");

            }

This is my second activity :

Intent detail= getIntent();
        nama=detail.getStringExtra("nama");
        alamat= detail.getStringExtra("alamat");
        ket= detail.getStringExtra("ket");
        img_id= detail.getStringExtra("img_id");
        telp= detail.getStringExtra("telp");
        begdate= detail.getStringExtra("begdate");
        enddate= detail.getStringExtra("enddate");

//      SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
//      settings.edit().clear().commit();
//      settings.edit().remove("nama").commit();
//      settings.edit().remove("alamat").commit();
//      settings.edit().remove("ket").commit();
//      settings.edit().remove("img_id").commit();
//      settings.edit().remove("telp").commit();
//      settings.edit().remove("begdate").commit();
//      settings.edit().remove("enddate").commit();
//      nama= settings.getString("nama", null);
//      alamat = settings.getString("alamat", null);
//      ket= settings.getString("ket", null);
//      img_id = settings.getString("img_id", null);
//      telp= settings.getString("telp", null);
//      begdate = settings.getString("begdate", null);
//      enddate = settings.getString("enddate", null);

I have force stopped, cleared cache of my application and uninstall it, but it still doesn't work. Is there any wrong with my method?

回答1:

Look at this code:

settings.edit()

Every time you use this code, there will be a new Editor. So You should do like this:

Editor editor = setting.edit();
editor.remove("nama");
......
editor.remove("enddate");
editor.commit();

Good Luck.enter code here



回答2:

If you want to remove the key from the SharePreference by clearing the editor

    editor.clear();
    editor.commit();