0

I have a ListActivity containing Items which are fetched from a JSON Object. When the user click one of the item, each item displays its details and attributes inside a DetailActivity class.

So I have one Activity to display the selected Item's attributes from the ListActivity. On the DetailActivity class there is a CheckBox, to mark the the Item as 'Favorite'. So every time the CheckBox inside the DetailActivity is checked, when the user opens the DetailActivity of that Item again, the state of the CheckBox is always checked.

I implemented so far by putting Boolean through SharedPreferences. But my method only saves the state of the DetailActivity class regardless which Item isChecked as favorite. So it doesn't save the state of a certain item, only the state of DetailActivity class.

How am I am able to do so?

Here is my snippet:

final CheckBox cb_fav = (CheckBox) findViewById(R.id.cb_tool_fav);
        cb_fav.setOnClickListener(this);
        boolean isChecked = getBooleanFromPreferences("isChecked");
        Log.i("start",""+isChecked);
        cb_fav.setChecked(isChecked);

        cb_fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
                Log.i("boolean",""+isChecked);
                DetailsActivity.this.putBooleanInPreferences(isChecked,"isChecked");

            }
            });

    }

     public void putBooleanInPreferences(boolean isChecked,String key){
            SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean(key, isChecked);
            editor.commit();        
        }
        public boolean getBooleanFromPreferences(String key){
            SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
            Boolean isChecked = sharedPreferences.getBoolean(key, false);
            return isChecked;       
        }

Here's the object class I'm fetching to my ListActivity, these are the attributes which are displayed inside the DetailsActivity class.

public class VideoLocation {

    public String deleted_at = null;
    public int documentary_video_length = -1;
    public int id = -1;
    public double latitude = 0d;
    public double longitude = 0d;
    public int position = -1;
    public String updated_at = null;
    public String name = null;
    public String text = null;
    public String documentary_video_url = null;
    public String documentary_thumbnail_url = null;
    public String audio_text_url = null;
    public Footage[] footages = null;

    public VideoLocation(){

    }
hectichavana
  • 1,436
  • 13
  • 41
  • 71

2 Answers2

1

Ofcourse, you need to save the checkbox state of each item.

From the attributes, i believe that "id" attribute is unique for each object. So you can save the state of the object by "id" attribute in following way:

putBooleanInPreferences(check_uncheck,String.valueOf(videoLocationObject.id));

Now whenever, you are displaying the object, you can retrieve the state in following way:

boolean check_uncheck=getBooleanFromPreferences(String.valueOf(videoLocationObject.id));

If "id" attribute is not unique, then select the attribute which is unique for each row as a key for your SharedPreferenceStorage.

Your code:

final CheckBox cb_fav = (CheckBox) findViewById(R.id.cb_tool_fav);

        boolean isChecked = getBooleanFromPreferences(String.valueOf(yourObject.id));
        Log.i("start",""+isChecked);
        cb_fav.setChecked(isChecked);
cb_fav.setOnClickListener(this);

        cb_fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
                Log.i("boolean",""+isChecked);
                putBooleanInPreferences(isChecked,String.valueOf(yourObject.id));

            }
            });

I hope this will be helpful to you.

Veer
  • 2,071
  • 19
  • 24
  • thank you for the answer. I got the big picture, but I kinda lost with the detail. Can you help me answering my question by providing the changes on my code above? That would be great. Thx – hectichavana May 07 '12 at 12:00
  • ah I got it, just changed all the 'key' with 'name' (of the Video Location). Thanks! – hectichavana May 07 '12 at 12:05
0

In my understanding u have to make multiple items as favorites.for this u have to use array of strings in shared preference or use a db to store the state of list items.if u are using array of shared prrefernce u can check out this link

Community
  • 1
  • 1
Jackson Chengalai
  • 3,907
  • 1
  • 24
  • 39