0

Overview

I have a List of items in my list view. When any item is clicked then next activity is started and in that activity there are few edit texts and three radio buttons in a radio group. I named these radio buttons as 'present','absent' and 'transfer'.

Problem and what I want to achieve

Now I managed to save the state of radio buttons so that if any of the radio button is checked and then activity closes then the radio button state will be saved. But my Problem is that whenever the value is stored when activity closes, all the list view items change their radiobutton states to that saved state.

Problem Explanation with Example

Lets say i have three items which are teachers in my list view. A, B and C. When i click on item A then activity named 'Attendance' started. Similarly when i clicked on B or C then activity Attendance started.Now if I click on item A and mark the attendance as Present and saved the state then when ever i open the item B or item C it will show me Present although i haven't change the value in item B or C. I just stored the value for item A but it also show changes in item B and C. I don't know what to do in this case to save radiobutton state for each listview item

What I Tried

This is what I am doing in my Oncreate method in Attendance activity.

SharedPreferences settings = getSharedPreferences("answers", MODE_PRIVATE);
    boolean r2achecked = settings.getBoolean("r2a",false);
    boolean r2bchecked = settings.getBoolean("r2b",false);
    boolean r2cchecked = settings.getBoolean("r2c",false);
    if (r2achecked == true)
    {
        one.setChecked(true);
    }
    else if (r2bchecked == true)
    {
        two.setChecked(true);
    }
    if (r2cchecked == true)
    {
        three.setChecked(true);
    }

and I am saving the states of radio buttons here in Attendance activity

save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            SharedPreferences settings = getSharedPreferences("answers", MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("r2a",one.isChecked());
            editor.putBoolean("r2b",two.isChecked());
            editor.putBoolean("r2c",three.isChecked());
            editor.apply();
            finish();
            }

Please Help anyone

  • why don't you just make a field in your data model? pass the same model to next activity, and make your radio button checked/unchecked on the basis of model value. – Junaid Hafeez Jun 07 '17 at 06:57
  • I'm not sure that `SharedPreferences` would be the best technology to do this - I mean it's simple, but only efficient if you will always have a small amount of data to handle (you should create `"answers1"`, `"answers2"`, etc., and [`SharedPreferences` was not created for this use case](https://stackoverflow.com/a/15698559/5596869))... A local database would be a better solution for the task you described above. My suggestions are [SQLite](https://developer.android.com/training/basics/data-storage/databases.html) or [Realm](https://realm.io/docs/java/latest/). – Geryson Jun 07 '17 at 07:18
  • @Geryson Sir you didnot get my point maybe. I am able to store the values but those stored value is showing in each list item –  Jun 07 '17 at 07:19
  • You can have only one value with the key `"answers"`. But you're loading **THAT VERY ONE** value into your activity (for example _Present_) **every time you start it** (and you are starting it _on every list item_). And because of this you will get every time the same result: _Present_. You are just overwriting the value of `"answers"` every time you save the preference, not creating another one - and this should be handled with the local database. – Geryson Jun 07 '17 at 07:30
  • Thankyou for explaining. I started working on your solution :) –  Jun 07 '17 at 07:49

0 Answers0