简体   繁体   中英

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.

 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. What you should be using is SQLlite database to maintain the current status of your TODO list. 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.

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.

->create Sqlite Database and create a table with all needed fields for your app and also create one field for checkbox state

->now load the Database while your checkbox activity loads and populate the check box state while your listview populates data from database.

->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.

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