how to save check box values of custom list view using shared preferences

半世苍凉 提交于 2019-12-13 04:46:08

问题


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


来源:https://stackoverflow.com/questions/27439645/how-to-save-check-box-values-of-custom-list-view-using-shared-preferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!