1

Hey guyz check it out what I am missing here cause of I am not able to fetch all my data from Shared Preferences. I am making such an tasklist application in which I am saving my data(means mytask) with a certain key and storing it in the shared Preferences and increments a variable for total count.

Check my Code(Whenever I click on addTask button the following code gets executed).

private void saveToSharedPreference(){
    sharedPre = getSharedPreferences("todoPref",Context.MODE_PRIVATE);
    editor = sharedPre.edit();
    String myKey = "key"+a;
    String myValue = et.getText().toString();
    editor.putString(myKey,myValue);
//        editor.putInt("totalTask", a);
    editor.commit();
} 

Now when I close the application and open it again the following code gets executed in order to load the data from shared preferences.

private  void loadData(){
    sharedPre = getSharedPreferences("todoPref",Context.MODE_PRIVATE);
    int p = 1;
    String myKey = "key"+p;
    while(sharedPre.getString(myKey,"") != null){
        Toast.makeText(this, sharedPre.getString(myKey,""),Toast.LENGTH_SHORT).show();
    }
}

but the problem is all it always returning null on all indexes. I don't know why I am getting this error. Please help me Thanks in advance Sarosh Madara

sarosh siddiq
  • 33
  • 1
  • 7
  • 1
    What's the value of `a` in `String myKey = "key"+a;`? – Jofre Mateu May 14 '15 at 14:45
  • 5
    In `String myKey = "key"+a;`. What value has `a`? And what type is it? Try to name your variables appropriately. Nobody else knows what `a` might be. – GiantTree May 14 '15 at 14:46
  • 1
    Are the values same for a & p variables??? – Pankaj May 14 '15 at 14:49
  • 1
    You save and load **different key names** (i.e.: you save "key0" and you load "key1"). If there is no 100% match, you won't retrieve the saved key. – Phantômaxx May 14 '15 at 14:49
  • Why do u want a while loop if key is same, that is ur not incrementing the index(p) in while or are u trying to create an infinite loop.... – DJphy May 14 '15 at 14:53
  • sorry jofre and gainttree the a is an int var which always increments... 1 – sarosh siddiq May 16 '15 at 07:35
  • Yes clairvoyant both are same... for the time being if I input 3 values key1 key2 and key3 then I want to retrieve key1 key2 and key3 thats why they would definitely be same. – sarosh siddiq May 16 '15 at 07:38

3 Answers3

1

Do like this -

1) save in shared preference like this -

private void saveToSharedPreference(){
    sharedPre = getSharedPreferences("todoPref",Context.MODE_PRIVATE);
    editor = sharedPre.edit();
    String myKey = "key";                        // removed +a
    String myValue = et.getText().toString();
    editor.putString(myKey,myValue);
//        editor.putInt("totalTask", a);
    editor.commit();
} 

2) load it like this -

 private void loadData() {
       sharedPre = getSharedPreferences("todoPref",Context.MODE_PRIVATE);
       int p = 1;
       String myKey = "key";  // removed "+p"
        String value = sharedPre.getString(myKey, ""); 

    }
Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
1

to load all saved values just use the following code:

private void loadData() {
        SharedPreferences sharedPre = getSharedPreferences("todoPref",android.content.Context.MODE_PRIVATE);
        Map<String,?> keys = sharedPre.getAll();
        for(Map.Entry<String,?> entry : keys.entrySet()){
                if (entry.getValue().toString().length() > 0) {
                    Log.d("map values",entry.getKey() + ": " + entry.getValue().toString());     
                }
         }
    }

I found it here but added a check to ignore null values.

to save values to Shared Preference I suggest using an instance of the current time for the key. no need to save any integer key values:

private void saveToSharedPreference(String myValue){
        SharedPreferences sharedPre = getSharedPreferences("todoPref",android.content.Context.MODE_PRIVATE);
        Editor editor = sharedPre.edit();
        String key = String.valueOf(Calendar.getInstance().getTimeInMillis());
        editor.putString(key,myValue);
        editor.commit();
    } 

so whenever you want to add values to Shared Preferences use:

saveToSharedPreference("myValue");

and to load them all use:

loadData();

The Map Interface

A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. read more...

SharedPreferences: Class Overview

Community
  • 1
  • 1
Karim
  • 766
  • 7
  • 9
  • Thanks for prompt help karim I would like to attempt your method but can you tell me what this mean Map I didn't get much about this till yet.. – sarosh siddiq May 16 '15 at 13:22
0

Don't compare with != null in while loop. Use below code and see if the problem solves:-

private  void loadData(){
    sharedPre = getSharedPreferences("todoPref",Context.MODE_PRIVATE);
    int p = 1;
    String myKey = "key"+p;
    String value = sharedPre.getString(myKey,""); 
    if(!value.equalsIgnoreCase("")){
        Toast.makeText(this, sharedPre.getString(myKey,""),Toast.LENGTH_SHORT).show();
    }
}
Pankaj
  • 7,908
  • 6
  • 42
  • 65