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

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

本文介绍了如何使用共享preferences保存的自定义列表视图复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使发射器。
  我已经都显示为app_label,图标,程序包和复选框的自定义列表。
  现在我想保存在共享preferences选定APP_NAME。
  我不是让如何通过它去。
  好心的帮助。

这是我的code:

 适配器=新ArrayAdapter< AppDetail>(MainActivity.this,
            android.R.layout.simple_list_item_multiple_choice,应用){        @覆盖
        公共查看getView(INT位置,查看convertView,
                            父母的ViewGroup){
            查看排= convertView;
            如果(行== NULL){
                。convertView = getLayoutInflater()膨胀(R.layout.list_item,父母,假);
            }            // check_checkbox();
             K = apps.get(位置);
            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);
            最后弦乐SS;
            SS = k.name;
             CH =(复选框)convertView.findViewById(R.id.c1);
             沟道.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
                @覆盖
                公共无效onCheckedChanged(CompoundButton buttonView,布尔B){                }
            });
            ch.setChecked(apps.get(位置).c1);            返回convertView;
        }
    };


解决方案

您可以在一个集中添加所有选中的应用程序名称,然后保存这一套共享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);

这篇关于如何使用共享preferences保存的自定义列表视图复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:35