共享preferences为某一个项目从ListActivity [英] SharedPreferences for a certain Item from a ListActivity

查看:132
本文介绍了共享preferences为某一个项目从ListActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有含有从JSON对象获取的项目一个ListActivity。
当用户单击该项目中的一个,每个项目显示其细节和DetailActivity类的内部属性。

所以我有一个活动,从ListActivity显示所选项目的属性。
在DetailActivity类有一个复选框,以纪念该项目为收藏。所以每次DetailActivity内的时间复选框被选中,当用户打开该项目的DetailActivity再次,CheckBox的状态始终选中。

我实现了迄今为止通过把布尔通过共享preferences。但我的方法只保存DetailActivity类的状态,无论哪个项目作为器isChecked喜爱。所以它不保存某个项目的状态,只有DetailActivity类的状态。

我如何我能这样做呢?

下面是我的片段:

 最后的CheckBox cb_fav =(复选框)findViewById(R.id.cb_tool_fav);
        cb_fav.setOnClickListener(本);
        布尔=器isChecked getBooleanFrom preferences(器isChecked);
        Log.i(开始,+器isChecked);
        cb_fav.setChecked(器isChecked);        cb_fav.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
            @覆盖
            公共无效onCheckedChanged(CompoundButton buttonView,
            布尔器isChecked){
                Log.i(布尔,+器isChecked);
                DetailsActivity.this.putBooleanIn preferences(器isChecked,器isChecked);            }
            });    }     公共无效putBooleanIn preferences(布尔器isChecked,串键){
            共享preferences共享preferences = this.get preferences(Activity.MODE_PRIVATE);
            共享preferences.Editor编辑=共享preferences.edit();
            editor.putBoolean(键,器isChecked);
            editor.commit();
        }
        公共布尔getBooleanFrom preferences(字符串键){
            共享preferences共享preferences = this.get preferences(Activity.MODE_PRIVATE);
            布尔=器isChecked共享preferences.getBoolean(键,FALSE);
            返回器isChecked;
        }

下面的对象类,我取出我的ListActivity,这些都是显示在DetailsActivity类中的属性。

 公共类VideoLocation {    公共字符串deleted_at = NULL;
    公众诠释documentary_video_length = -1;
    公众诠释的id = -1;
    公共双纬度= 0D;
    公共双经度= 0D;
    公众诠释位置= -1;
    公共字符串的updated_at = NULL;
    公共字符串名称= NULL;
    公共字符串文本= NULL;
    公共字符串documentary_video_url = NULL;
    公共字符串documentary_thumbnail_url = NULL;
    公共字符串audio_text_url = NULL;
    公共素材[]进尺= NULL;    公共VideoLocation(){    }


解决方案

Ofcourse,你需要保存每个项目的复选框状态。

从属性的,我相信,id属性是每个对象唯一的。

:那么您可以通过以下方式id属性保存对象的状态

  putBooleanIn preferences(check_uncheck,将String.valueOf(videoLocationObject.id));

现在何时何地,你都显示对象,可以检索方式如下状态:

 布尔check_uncheck = getBooleanFrom preferences(将String.valueOf(videoLocationObject.id));

如果id属性不是唯一的,然后选择它为每个行作为你的共享preferenceStorage关键独特属性。

您code:

 最后的CheckBox cb_fav =(复选框)findViewById(R.id.cb_tool_fav);        布尔=器isChecked getBooleanFrom preferences(将String.valueOf(yourObject.id));
        Log.i(开始,+器isChecked);
        cb_fav.setChecked(器isChecked);
cb_fav.setOnClickListener(本);        cb_fav.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
            @覆盖
            公共无效onCheckedChanged(CompoundButton buttonView,
            布尔器isChecked){
                Log.i(布尔,+器isChecked);
                putBooleanIn preferences(器isChecked,将String.valueOf(yourObject.id));            }
            });

我希望这将有助于你。

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.

这篇关于共享preferences为某一个项目从ListActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆