简体   繁体   中英

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 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(){

    }

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.

以我的理解,您必须将多个项目作为收藏夹。为此,您必须使用共享首选项中的字符串数组或使用db存储列表项的状态。如果您使用共享优先级数组,则可以查看此链接

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM