且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用共享preferences保存的自定义列表视图复选框值

更新时间:2022-10-21 12:56:13

您可以在一个集中添加所有选中的应用程序名称,然后保存这一套共享prefrences: -

 设置<串GT;设置=新的HashSet<串GT;();
沟道.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
            @覆盖
            公共无效onCheckedChanged(CompoundButton buttonView,布尔B){
             如果()
            {
             set.add(ch.getText());
            }
             其他
            {
            set.remove(ch.getText());
            }        });

然后在您的共享prefrences投入使用编辑器设置

  editor.putStringSet(appnames,设置);

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


    }; 

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