简体   繁体   English

如何使用共享首选项在listview中保存复选框状态

[英]How to save checkbox state in listview using sharedpreference

I have checkboxes in listview, i want checkbox states to be saved when i click on it,now when i resume my app ,all the checkboxes will be unchecked.I am trying to develop TODO List app where list row textview will be striken and checkbox will be checked, how can i save checkbox state and striken textview into sharedpreference and load. 我在listview中有复选框,我希望在我点击它时保存复选框状态,现在当我恢复我的应用程序时,所有复选框都将被取消选中。我正在尝试开发TODO列表应用程序列表行textview将被删除和复选框将被检查,我如何将复选框状态和striken textview保存到共享首选项和加载。

 protected void onCreate(Bundle saved) {
        super.onCreate(saved);
        setContentView(R.layout.cbox_list);
        Listvw = (ListView) findViewById(R.id.clistvw);
        Listvw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                cText = (TextView) view.findViewById(R.id.ctext);
                cBox = (CheckBox) view.findViewById(R.id.cbox);
                cBox.setChecked(true);
                //Toast.makeText(getActivity(),"Clicked",Toast.LENGTH_LONG).show();
                cText.setPaintFlags(cText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                //boolean value=cBox.isChecked();
                int b = Listvw.getAdapter().getCount();
                for (int i1 = 0; i1 < b; i1++) {
                    if (cBox.isChecked()) {
                        SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(CBox_InListView.this);
                        SharedPreferences.Editor edit = spf.edit();
                        edit.putBoolean("name"+i1, cBox.isChecked());
                        edit.commit();
                    }
                }
            }
        });

        model = new CheckModel[12];
        model[0] = new CheckModel("Item1", 0);
        model[1] = new CheckModel("Item", 0);
        model[2] = new CheckModel("Item", 0);
        model[3] = new CheckModel("Item", 0);
        model[4] = new CheckModel("Item", 0);
        model[5] = new CheckModel("Item", 0);
        model[6] = new CheckModel("Item", 0);
        model[7] = new CheckModel("Home Head", 0);
        model[8] = new CheckModel("Item", 0);
        model[9] = new CheckModel("Item", 0);
        model[10] = new CheckModel("Item", 0);
        model[11] = new CheckModel("Item", 0);
        CustomAdapter adpter = new CustomAdapter(this, model);
       int c=Listvw.getAdapter().getCount();
        for(int i=0;i<c;i++)
        {
            SharedPreferences pf=PreferenceManager.getDefaultSharedPreferences(CBox_InListView.this);
            boolean chkbx=pf.getBoolean("name"+i,false);
            if(chkbx){
                cBox.setChecked(true);
            }else{
                cBox.setChecked(false);
            }
        }
        Listvw.setAdapter(adpter);

Using SharedPreferences for large data is not a good approach over all. 对大数据使用SharedPreferences并不是一个好方法。 What you should be using is SQLlite database to maintain the current status of your TODO list. 你应该使用的是SQLlite数据库来维护你的TODO列表的当前状态。 Go for setting title of your todo as primary key and you should have a boolean variable in your CheckModel class that defines if a checkbox is checked or not. 将todo的标题设置为主键,并且CheckModel类中应该有一个布尔变量,用于定义是否选中复选框。

Then you have to implement logic in getView method of adapter to set checkboxes to checked or unchecked state and not the way that you are doing it right now. 然后,您必须在适配器的getView方法中实现逻辑,以将复选框设置为已选中或未选中状态,而不是您现在正在执行此操作的方式。

->create Sqlite Database and create a table with all needed fields for your app and also create one field for checkbox state - >创建Sqlite数据库并创建一个包含应用程序所需字段的表,并为复选框状态创建一个字段

->now load the Database while your checkbox activity loads and populate the check box state while your listview populates data from database. - >现在在加载复选框活动时加载数据库,并在listview填充数据库中的数据时填充复选框状态。

->While user select or de select checkbox, Listen for check box state and update the database - >当用户选择或取消选中复选框时,侦听复选框状态并更新数据库

Using shared preference for each list item is not good, database is good option. 对每个列表项使用共享首选项并不好,数据库是不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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