Skip to content Skip to sidebar Skip to footer

Sharedpreferences For A Certain Item From A Listactivity

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 DetailActiv

Solution 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(newCompoundButton.OnCheckedChangeListener(){
            @OverridepublicvoidonCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
                Log.i("boolean",""+isChecked);
                putBooleanInPreferences(isChecked,String.valueOf(yourObject.id));

            }
            });

I hope this will be helpful to you.

Solution 2:

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

Post a Comment for "Sharedpreferences For A Certain Item From A Listactivity"