mardi 24 novembre 2015

Save listview checkbox values to sharedpreferences

I need to save the checkbox values so that when the app is exited, the checked boxes are still checked. How do I do this? Here is my entire code:

public class Surv_list extends Fragment {
    public View row;

    final String[] OPSys = new String[]{"1", "2","3", "4"
    };
    ListView myList;
    CheckBox xbx;
    Button getChoice, clearAll;
    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyUserChoice" ;
    ArrayList<String> selectedItems = new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.listlay, container, false);

        myList = (ListView)rootView.findViewById(R.id.list);
        ListView list = (ListView) rootView.findViewById(R.id.list);
        CheckBox xbxo = (CheckBox) rootView.findViewById(R.id.cbx);
        clearAll = (Button)rootView.findViewById(R.id.clearall);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.task_item, R.id.task_description, OPSys);
        myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        myList.setAdapter(adapter);
        list.setAdapter(adapter);
        sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        if(sharedpreferences.contains(MyPREFERENCES)){
            LoadSelections();
        }
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {

                SaveSelections();

            }

        });


        clearAll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
// TODO Auto-generated method stub
                ClearSelections();
            }
        });
return rootView;
    }

    private void SaveSelections() {
// save the selections in the shared preference in private mode for the user

        SharedPreferences.Editor prefEditor = sharedpreferences.edit();
        String savedItems = getSavedItems();
        prefEditor.putString(MyPREFERENCES.toString(), savedItems);
        prefEditor.commit();
    }

    private String getSavedItems() {
        String savedItems = "";
        int count = this.myList.getAdapter().getCount();
        for (int i = 0; i < count; i++) {
            if (this.myList.isItemChecked(i)) {
                if (savedItems.length() > 0) {
                    savedItems += "," + this.myList.getItemAtPosition(i);

                } else {
                    savedItems += this.myList.getItemAtPosition(i);

                }
            }
        }
        return savedItems;
    }

    private void LoadSelections() {
// if the selections were previously saved load them

        if (sharedpreferences.contains(MyPREFERENCES.toString())) {

            String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), "");
            selectedItems.addAll(Arrays.asList(savedItems.split(",")));

            int count = this.myList.getAdapter().getCount();

            for (int i = 0; i < count; i++) {
                String currentItem = (String) myList.getAdapter()
                        .getItem(i);
                if (selectedItems.contains(currentItem)) {
                    myList.setItemChecked(i, true);
                } else {
                    myList.setItemChecked(i, false);
                }
            }
        }
    }
    private void ClearSelections() {
// user has clicked clear button so uncheck all the items
        int count = this.myList.getAdapter().getCount();
        for (int i = 0; i < count; i++) {
            this.myList.setItemChecked(i, false);
        }
// also clear the saved selections
        SaveSelections();
    }

}

Originally, I was using android.R.layout.simple_list_item_multiple_choice for the checkboxes on the listview but I've decided to use a custom one instead and now it doesn't save to SharedPreferences. Can anyone please show me how to solve this?




Aucun commentaire:

Enregistrer un commentaire