简体   繁体   中英

Storing Data with SharedPreferences in Android

I'm new to Android programming and I've never dabbled in saving values or settings before. I've taken a look at Storage Options and I've tried to Implement SharedPreferences. And from my point of view, when I create a new event, I tell it to pull some data, and set a TextView to the value. However it never sets the TextView; and I can't seem to figure out what is wrong.

createEvent This is where I'm creating the values I want to stored.

@Override
protected void onCreate(Bundle savedInstanceState) {
    LayoutInflater inflater = this.getLayoutInflater();
    final View vView = inflater.inflate(R.layout.fragment_main, null);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    SharedPreferences settings = getPreferences(0);
    int iEventCounter = settings.getInt("eventCounter", 0);
    if (iEventCounter > 0){
        String eventName1 = settings.getString("event1Name", "Name of Event");
        TextView eventLabelName = (TextView)vView.findViewById(R.id.section_label);
        eventLabelName.setText(eventName1);
    }
    else if (iEventCounter == 0 || iEventCounter < 0){
        TextView eventLabelName = (TextView)vView.findViewById(R.id.section_label);
        eventLabelName.setText("There is no event stored in memory.");
    }
}

eventAdd (Here is where I commit data to SharedPreferences

public int eventAdd(String sName, String sDate) {
    LayoutInflater inflater = this.getLayoutInflater();
    final View vView = inflater.inflate(R.layout.fragment_main, null);
    TextView eventLabelName = (TextView)vView.findViewById(R.id.testTextView);
    eventLabelName.setText("test"); //Never changes
    SharedPreferences settings = getPreferences(0); //Create SharedPreferences
    SharedPreferences.Editor editor = settings.edit(); //Create editor
    iEventCounter = settings.getInt("eventCounter", 0);
    iEventCounter = iEventCounter+1;
    editor.putInt("eventCounter", iEventCounter); //Put values for first Event into SharedPreferences
    editor.putString("event1Name", sName);
    editor.putString("event1Date", sDate);
    // Commit the edits!
    editor.commit();

    //Change Label based on number of Events thus far.
    if (iEventCounter > 0){
        String eventName1 = settings.getString("event1Name", "Name of Event");

        eventLabelName.setText(eventName1);
    }
    else if (iEventCounter == 0 || iEventCounter < 0){
        eventLabelName.setText("There is no event stored in memory.");
    }
    return iEventCounter;
    //eventLabelName.setText(iEventCounter); Doesn't do anything, doesn't change Text.
}

I don't get an error, the app doesn't crash, it just doesn't work. Can anyone see anything wrong here? Any help would be greatly appreciated.

You are inflating a view but not setting it to activity's content. So you don't see any change .

final View vView = inflater.inflate(R.layout.fragment_main, null); 
setContentView(R.layout.activity_main);

Maybe you should use;

setContentView(mView);

Or if you want to set activity_main to your activity's content, change;

vView.findViewById

to

findViewById

Try to refactor your code. There are some unnecessary codes.

Try changing

SharedPreferences settings = getPreferences(0);

in both methods to

SharedPreferences settings = getSharedPreferences("preferenceName", 0);

Your code is kinda strange at some points.

In the first place, you could turn this code

SharedPreferences settings = getPreferences(0);
    int iEventCounter = settings.getInt("eventCounter", 0);
    if (iEventCounter > 0){
        String eventName1 = settings.getString("event1Name", "Name of Event");
        TextView eventLabelName = (TextView)vView.findViewById(R.id.section_label);
        eventLabelName.setText(eventName1);
    }
    else if (iEventCounter == 0 || iEventCounter < 0){
        TextView eventLabelName = (TextView)vView.findViewById(R.id.section_label);
        eventLabelName.setText("There is no event stored in memory.");
    }

into the next one:

SharedPreferences settings = getSharedPreferences("preferences", MODE_PRIVATE); //Always use the name of the variable to know its meaning. 
    int iEventCounter = settings.getInt("eventCounter", 0);
    if (iEventCounter > 0){
        String eventName1 = settings.getString("event1Name", "Name of Event");
        TextView eventLabelName = (TextView)vView.findViewById(R.id.section_label);
        eventLabelName.setText(eventName1);
    }
    else { // We know what the other cases are, more readable
        TextView eventLabelName = (TextView)vView.findViewById(R.id.section_label);
        eventLabelName.setText("There is no event stored in memory.");
    }

and this other piece:

   SharedPreferences settings = getPreferences(0); //Create SharedPreferences
            SharedPreferences.Editor editor = settings.edit(); //Create editor
            iEventCounter = settings.getInt("eventCounter", 0);
            iEventCounter = iEventCounter+1;
            editor.putInt("eventCounter", iEventCounter); //Put values for first Event into SharedPreferences
            editor.putString("event1Name", sName);
            editor.putString("event1Date", sDate);
            // Commit the edits!
            editor.commit();

            //Change Label based on number of Events thus far.
            if (iEventCounter > 0){
                String eventName1 = settings.getString("event1Name", "Name of Event");

                eventLabelName.setText(eventName1);
            }
            else if (iEventCounter == 0 || iEventCounter < 0){
                eventLabelName.setText("There is no event stored in memory.");

into this piece:

SharedPreferences settings = getSharedPreferences("preferences", MODE_PRIVATE); //Same as before
    iEventCounter = settings.getInt("eventCounter", 0); // We first get the value, then we edit
    SharedPreferences.Editor editor = settings.edit(); //Create editor

    editor.putInt("eventCounter", ++iEventCounter); //Pre-increment first increments the variable value then does whatever you wanted to.
    editor.putString("event1Name", sName);
    editor.putString("event1Date", sDate);
    // Commit the edits!
    editor.commit();

    //Change Label based on number of Events thus far.
    if (iEventCounter > 0){
        String eventName1 = settings.getString("event1Name", "Name of Event");
        eventLabelName.setText(eventName1);
    }
    else { // Same as before
        eventLabelName.setText("There is no event stored in memory.");

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