質問

I'm not sure how to word this but I'm having issues accessing Shared Preferences under MODE_PRIVATE.

I'm implementing a Spinner like so:

setSessions = (Spinner)findViewById(R.id.numSessions);
setSessions.setOnItemSelectedListener(new CustomOnItemSelectedListener());

The Custom listener is like so:

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {

        SharedPreferences defaultPrefs;
        defaultPrefs = getSharedPreferences("Defaults", MODE_PRIVATE);
        String savedSessions = defaultPrefs.getString("tcpCapSessions", "None Set");

        if (savedSessions != parent.getItemAtPosition(pos).toString()) {
        final Editor defaultEdit = defaultPrefs.edit();  
        defaultEdit.putString("tcpCapSessions", parent.getItemAtPosition(pos).toString());              // Writes the key "Default Server" along with Server Name chosen (as the value).
        defaultEdit.commit();
        }

        Toast.makeText(parent.getContext(), 
                "Sessions set to: " + parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

I'm getting an error on the "MODE_PRIVATE" when getting SharedPreferences....

enter image description here

I simply want to update a preference depending on the users selection. I've tried extending the Activity which gets rid of the error but the app will still crash on first launch.

I've checked out Use sharedpreferences inside class but it hasn't helped me resolve the issue, may be it's missing something as it appears like the same issue as myself.

役に立ちましたか?

解決

MODE_PRIVATE is a static member of Context. It's actually described pretty good here.

change it to Context.MODE_PRIVATE

他のヒント

Hi Genius try this..

  1. Shared Preferences allow you to save and retrieve data in the form of key,value pair.
  2. You have to call a method getSharedPreferences() that returns a SharedPreferences instance pointing to the file that contains the values of preferences.

     SharedPreferences sharedpreferences = getSharedPreferences(YOUR_PREFERENCE_NAME, Context.MODE_PRIVATE);
    

The first parameter is the Name and the second parameter is the MODE.

Name:

Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).

Mode:

(Operating Mode)

  • 0 or MODE_PRIVATE for the default operation.
  • MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.
  • MODE_MULTI_PROCESS can also be used if multiple processes are mutating the same SharedPreferences file.

Example:

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

  //Initialize SharedPreferences.

    public static SharedPreferences.Editor editUserSelection;
public static SharedPreferences prefUserSelection;
public static final String GET_USER_PREF = "get_my_user";

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {

     //save user selected position.
    prefUserSelection = getApplicationContext().getSharedPreferences(GET_USER_PREF, 0);
    editUserSelection= prefUserSelection.edit();
     editUserSelection.putInt("user", pos);
     editUserSelection.commit();
     }

For retrieve the user position,

prefUserSelection = getApplicationContext().getSharedPreferences(GET_USER_PREF, 0);
int userSelected = prefUserSelection .getInt("user", 0); //0 is the default value
Toast.makeText(getActivity(), "User Selected Position: "+userSelected ,Toast.LENGTH_SHORT).show();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top