0

我有一个 ListActivity 包含从 JSON 对象中获取的项目。当用户单击其中一个项目时,每个项目都会在 DetailActivity 类中显示其详细信息和属性。

所以我有一个 Activity 来显示 ListActivity 中所选项目的属性。在 DetailActivity 类上有一个 CheckBox,用于将 Item 标记为“Favorite”。所以每次勾选DetailActivity里面的CheckBox,当用户再次打开那个Item的DetailActivity时,总是勾选CheckBox的状态。

到目前为止,我通过 SharedPreferences 放置布尔值来实现。但是我的方法只保存了 DetailActivity 类的状态,而不管哪个 Item 被选中为收藏。所以它不保存某个项目的状态,只保存 DetailActivity 类的状态。

我怎么能这样做?

这是我的片段:

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;       
        }

这是我要提取到我的 ListActivity 的对象类,这些是在 DetailsActivity 类中显示的属性。

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

    }
4

2 回答 2

1

当然,您需要保存每个项目的复选框状态。

从属性来看,我相信“id”属性对于每个对象都是唯一的。因此,您可以通过以下方式通过“id”属性保存对象的状态:

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

现在,每当您显示对象时,您都可以通过以下方式检索状态:

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

如果“id”属性不是唯一的,则选择每行唯一的属性作为 SharedPreferenceStorage 的键。

你的代码:

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));

            }
            });

我希望这对你有帮助。

于 2012-05-07T11:27:23.687 回答
0

据我了解,您必须将多个项目设为收藏夹。为此,您必须在共享首选项中使用字符串数组或使用数据库来存储列表项的状态。如果您正在使用共享首选项数组,您可以查看此链接

于 2012-05-07T11:27:06.453 回答