【问题标题】:how to save check box values of custom list view using shared preferences如何使用共享首选项保存自定义列表视图的复选框值
【发布时间】:2015-02-10 22:16:37
【问题描述】:

我正在尝试制作启动器。 我已经显示了带有 app_label、icon、package_name 和复选框的自定义列表。 现在我想将选定的 app_name 保存在共享首选项中。 我不知道如何通过它。 请帮忙。

这是我的代码:

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


    }; 

【问题讨论】:

  • 那么您是否使用此代码获取选定的应用程序?

标签: android listview checkbox sharedpreferences android-custom-view


【解决方案1】:

您可以将所有选中的应用名称添加到一个集合中,然后将此集合保存在 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());
            }

        });

然后在您的 sharedprefrences 中使用编辑器放置集合

 editor.putStringSet("appnames",set);

【讨论】:

  • 该集合正在保存..但是当我滚动列表时..它消失了...实际上我应该调用我的函数来从共享首选项中加载数据。
  • 由于视图回收,您遇到了这个问题看看这个stackoverflow.com/questions/27438858/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 2016-12-26
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 2022-01-15
相关资源
最近更新 更多