简体   繁体   中英

Shared Preferences save and retrieve data

I'm using Shared Preferences to save data from an AutoCompleteTextView. When the user writes something in this AutoCompleteTextView, he can click a button in order to save what he just wrote (so that he doesn't have to write it every time).

Here's what my code looks like:

private AutoCompleteTextView autoComplete = null;
String nameFile = "Web service data";
String myData = "";
SharedPreferences pref;
Editor editor;
String channel = "";
String[] valuesArray = {channel};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

    pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
    editor = pref.edit();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, valuesArray);
    autoComplete = (AutoCompleteTextView) findViewById(R.id.autocompletion);
    autoComplete.setAdapter(adapter);

    Button add = (Button) findViewById(R.id.add); 
    add.setOnClickListener(sendForm2);
    Button remove = (Button) findViewById(R.id.remove);
    remove.setOnClickListener(sendForm2);

    channel = pref.getString(nameFile, null);
}


OnClickListener sendForm2 = new OnClickListener() {
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                myData = autoComplete.getText().toString();
                editor.putString(nameFile, myData);
                editor.commit();
            break;

            case R.id.remove:
                editor.remove(nameFile);
                editor.commit();
            break;   
        }
    }   
};

The problem is, the Shared Preferences doesn't save any data in channel at all. Even when I close the application and restart it. Any clue or idea how to resolve this problem?

First thing I would try would be to add a

Log.d("OnCreate", "channel : "+ channel);

in the onCreate just after

channel = pref.getString(nameFile, null);

to see if you have something inside.

If you don't, this really means that sharedpref are not saved.

In this case I would try to bring back the :

pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
editor = pref.edit();

just before the

switch (v.getId()) {

I remember reading that sometimes depending on what you are doing with your activities, the sharedpref editor you create can be "lost" and not be related to anything later in the code.

Use the following method to save and retrive String prefrence.

//save the prefrence by pasing key and data

public static void SavePrefrence(Context ctx, String Key, String value) {
    ctx.getSharedPreferences("mypref", ctx.MODE_PRIVATE)
            .edit().putString(Key, value).commit();
}

//get the prefrence by passing your key

public static String getPrefrence(Context ctx, String key) {
        SharedPreferences pref = ctx.getSharedPreferences(
                "mypref", ctx.MODE_PRIVATE);
        String result = pref.getString(key, null);
        return result;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM