1

i have a main activity which is creating shared preference and assigning it to a value each hour. The shared preferences over there are getting created, and are passed to a listview which is using an adapter class to populate and pulls the values from the same shared preferences And in the second activity the code for retreiving is written in on resume so that it can show updated numbers when back is pressed. which looks something like this, onlisttem click listener forwards the control to this activity which is suppose to add or substract value and update the shared preferences, i am updating it in onpause and onBackPressed, coz i think its too much to put in an onClickListener, here's the code for Last counter activity. The problem is the shared preferences is not getting updated.

ImageButton add,sub,push;
TextView countkeeper, countpusher;
String []hours={"8:30 - 9:30","9:30 - 10:30","10:30 - 11:30","11:30 - 12:30","12:30 - 1:30","1:30 - 2:30","2:30 - 3:30","3:30 - 4:30","4:30 - 5:30"};
int lip,c,target;
Intent i;
SharedPreferences sp,sp1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_counter);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    add=(ImageButton)findViewById(R.id.imageButton1);
    sub=(ImageButton)findViewById(R.id.imageButton2);
    push=(ImageButton)findViewById(R.id.imageButton3);
    countkeeper=(TextView)findViewById(R.id.countkeeper);
    countpusher=(TextView)findViewById(R.id.countpusher);

    sp= getSharedPreferences("Count", MODE_APPEND);
    Log.d("CurrentCountFor"+hours[lip],""+sp.getInt("CurrentCountFor"+hours[lip], 10));
    c=sp.getInt("CurrentCountFor"+hours[lip], 10);
    target=sp.getInt("TargetFor"+hours[lip], 10);



    i=getIntent();
    lip= i.getIntExtra("lip", 0);


    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            c = c+1;
            countkeeper.setText("Count for "+hours[lip]+" is "+c+"/"+target);



        }
    });

    sub.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            c=c-1;
            countkeeper.setText("Count for "+hours[lip]+" is "+c+"/"+target);


        }
    });
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    sp1= getSharedPreferences("Count", MODE_APPEND);
    SharedPreferences.Editor edit=sp1.edit();
    edit.putInt("CurrentCountFor"+hours[lip], c);
    sp1.edit().commit();

};

@Override
protected void onPause() {
    super.onPause();
    sp1= getSharedPreferences("Count", MODE_APPEND);
    SharedPreferences.Editor edit=sp1.edit();
    edit.putInt("CurrentCountFor"+hours[lip], c);
    sp1.edit().commit();


};

Help anyone please.

1 Answers1

1

Replace sp1.edit().commit(); with edit.commit();

You have the same issue twice.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • although this is changing the values in shared prefernces, when i come back from the list view the next hours count i.e. c value is sticking, which means if a press add button 2 times in 8:30 to 9:30, and exit and come back to 9:30 to 10:30 hour list item c's value which is by default suppose to be 0 or what ever was suppose to be that hours count, is now 2 or same as something that was last there in that activity – Osman Shareef Apr 13 '14 at 18:01
  • I'm not sure I'm understanding the problem well... But it seems to me that you don't reload the saved values. Am I missing something? – Phantômaxx Apr 13 '14 at 18:08
  • Yup the values aren't reloading, its sticking to the previous values which were saved. – Osman Shareef Apr 13 '14 at 18:14
  • Lemme give you a better understanding of what the app is doing. ok here's what the app is intended to do,imagine a factory which makes chairs, and an employee is given everyday's target to make chairs, at the starting of the day, the employee enters his targets and the app divides it into hours (9 hours), so that he finishes that many chairs in that hour, and doesn't get over burdened by the EOD, – Osman Shareef Apr 13 '14 at 18:15
  • So there are 3 activities, main_activity, takes count and passes control to the list view, 2nd activity, listview item takes to 3rd activity is a counter where a value can be added or substracted, the problem is , if i set a target for 90. which is 10 per hour, and click on 8:30 to 9:30 list item, press add 3 times, which makes the value of "c" =3; if i to the 9:30 to 10:30 list item, it shows the value of c same as 8:30 to 9:30 's value. – Osman Shareef Apr 13 '14 at 18:20
  • Does this have anything to do with the SharedPreferences loading and saving? – Phantômaxx Apr 13 '14 at 18:24
  • Yes the values are stores in Shared Preferences, its not reloading when i open another list item, it is as same as the previous value, how do i reload it? – Osman Shareef Apr 13 '14 at 18:28
  • So, when you are in the next Activity (the one called from the ListView and AFTER the values have been saved), just reload them. I think you miss this step. Something similar to what you already do in your code around this line: `c=sp.getInt("CurrentCountFor"+hours[lip], 10);` – Phantômaxx Apr 13 '14 at 18:31
  • This line is there in the onCreate of Counter class, tried adding it to onPause and onBackpressed as well, the value wont update, it has to fetch the new values of that particular hour. – Osman Shareef Apr 13 '14 at 18:35
  • No, you have to save the values as soon as you modify them. Then, reload them as soon as you need them (in the onCreate function?). do you need the values to be persisted through sessions? If not, just use some shared variables between your Activities. – Phantômaxx Apr 13 '14 at 18:39
  • i think, the users might want to end the session and come back again and continue with the session for which a button the main activity is present which points directly to the list view with previous values stored in shared prefernces, storing shared variables won't help. So, do i add the sharedpreferenes editor inside onClick events itself? – Osman Shareef Apr 13 '14 at 18:43
  • 1
    I normally add some shared methods to a class that I use to load and save the values from and to the SharedPreferences. My methods have names like settings_readAll(), settings_writeInt(key, value) and so on. I use the writeXYZ methods only when needed, and the settings_readAll everytime I made some changes. Note that reading sets some shared variables, in order to allow me to read them in other Activities too. – Phantômaxx Apr 13 '14 at 19:04
  • that's so much better, but will have to re-write pretty much everything. Thanks a lot mate. – Osman Shareef Apr 13 '14 at 19:15
  • 1
    You are welcome. I hope I gave you a good suggestion on how to move in the right direction. – Phantômaxx Apr 13 '14 at 19:20
  • ofcourse you did, thank you so much for your time. Cheers and god bless. One last thing, how do i make this app, have its backlight on and not make it lock screen after sleep time for as long as the user is in it? – Osman Shareef Apr 13 '14 at 19:32
  • 1
    Keeping the screen on isn't a good practice, it can disturb the user. But... here it is: http://stackoverflow.com/questions/5712849/how-do-i-keep-the-screen-on-in-my-app – Phantômaxx Apr 14 '14 at 08:20