how to save check box values of custom list view u

2019-08-28 10:42发布

问题:

i am trying to make launcher. i have have displayed the custom list with app_label,icon,package_name and check box. now i want to save selected app_name in shared preferences. i m not getting how to go through it. kindly help.

here is my code:

   adapter = new ArrayAdapter<AppDetail>(MainActivity.this,
            android.R.layout.simple_list_item_multiple_choice, apps) {

        @Override
        public View getView(int position, View convertView,
                            ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                convertView = getLayoutInflater().inflate( R.layout.list_item,parent,false);
            }

            //  check_checkbox();
             k=apps.get(position);
            ImageView appIcon = (ImageView) convertView
                    .findViewById(R.id.item_app_icon);
            appIcon.setImageDrawable(k.icon);

            TextView appLabel = (TextView) convertView
                    .findViewById(R.id.item_app_label);
            appLabel.setText(k.label);

            TextView appName = (TextView) convertView
                    .findViewById(R.id.item_app_name);
            appName.setText(k.name);


            final String ss;
            ss = k.name;
             ch = (CheckBox) convertView.findViewById(R.id.c1);
             ch .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean b) {

                }
            });
            ch.setChecked(apps.get(position).c1);

            return convertView;
        }


    }; 

回答1:

you can add all the checked app names in a set and then save this set in sharedprefrences:-

Set<String> set =new HashSet<String>();
ch .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean b) {
             if()
            {
             set.add(ch.getText());
            }
             else 
            {
            set.remove(ch.getText());
            }

        });

then in your sharedprefrences put the set using the editor

 editor.putStringSet("appnames",set);