0

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.

Cistoran
  • 1,587
  • 15
  • 36
  • 54
  • Cannot spot the error but consider creating a helper class like this: http://stackoverflow.com/questions/19038823/saving-textview-in-a-fragment-when-rotating-screen/19039229#19039229 Works well for me – cYrixmorten Nov 17 '13 at 00:11

3 Answers3

2

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.

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • I did want to set it to activity_main. I can't believe I didn't see that before. Thanks for your help. – Cistoran Nov 17 '13 at 00:09
0

Try changing

SharedPreferences settings = getPreferences(0);

in both methods to

SharedPreferences settings = getSharedPreferences("preferenceName", 0);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
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.");
ervaka
  • 307
  • 1
  • 5