Brendom
Brendom

Reputation: 123

Delete Shared Preferences in android

This is how i add shared preferences

    ct = sp.getInt("count", 0);
    if (ct > 0) {
        for (int i = 0; i <= ct; i++) {
            list.add(sp.getString("Value[" + i + "]", ""));
        }
    }
        adp = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, list);
        Listtt.setAdapter(adp);
        btnad.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                sped.putString("Value[" + ct + "]", editText.getText().toString());
                sped.commit();
                list.add(sp.getString("Value[" + ct + "]", ""));
                ct += 1;
                sped.putInt("count", ct);
                adp.notifyDataSetChanged();

            }
        });

And by this i can successfully delete Sharedprefrences Value and remove it from list

 sp.edit().remove("key_to_remove").apply();
             adp.remove("name_to_rmv");
             adp.notifyDataSetChanged();

Now my problem is that it leaves the blank space when i call it back just like closing activity and opening it again the value i have deleted has the blank space . Just like this image below i have deleted " two " but when i close and open my application it gives me blank space

how i fill adapter

 ct = sp.getInt("count", 0);
        if (ct > 0) {
            for (int i = 0; i <= ct; i++) {
                list.add(sp.getString("Value[" + i + "]", ""));
            }
        }
            adp = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, list);
            Listtt.setAdapter(adp);

.Example output

Upvotes: 1

Views: 391

Answers (1)

Kasun Dissanayake
Kasun Dissanayake

Reputation: 506

I've met the same problem. I figured out a way to do this. This may not be the best way but it do what needs to be done. You will have to decrease each and every value after the deleted value in SP by 1

if you have values in sp like

"value[0]","one"
"value[1]","two"
"value[2]","three"
"value[3]","four"  

after deleted "value[1]", it would be

"value[0]","one"
"value[2]","three"
"value[3]","four"
(that's why it returns null string when you try to fetch "value[1]" because there are no such a value)

so make them a continues by inserting "value[1]". That means modify your existing value ("value[2]","three") as ("value[1]","three"). and apply this to other values also. you can simply do this by with a for loop. Then your final values would be like this.

"value[0]","one"
"value[1]","three"
"value[2]","four"

As you can see there won't be any blank spaces when you reading back. You will have to decrease counter by 1 because now there are only 3 values.

    void deleteItem(int element_num,int counter,SharedPreferences sp,ArrayAdapter<String> adp){


        //element_num is the element number that you want to delete
        String name_to_rmv=sp.getString("Value[" + element_num + "]", ""); //backup the value that needs to be deleted

        SharedPreferences.Editor editor=sp.edit();

        for(int i=element_num;i<counter;i++)
            editor.putString("Value[" + i + "]", sp.getString("Value[" + (i+1) + "]", "")); //read i+1 value and store it to i

        counter--;              //decrease counter
        editor.putInt("count", counter);
        editor.commit();

        adp.remove(name_to_rmv);    
        adp.notifyDataSetChanged();
    }

Upvotes: 1

Related Questions