自定义ListView不会取消选中notifyDataSetChanged之后的所有复选框

时间:2012-04-18 13:55:26

标签: android listview checkbox

我正在开发Android 3.1应用程序。我是Android开发的新手。

我有ListActivity个项目定义如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <CheckBox
        android:id="@+id/itemCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

ListActivity显示表单的列表。用户可以选择(检查其checkbox)一个或多个表格并下载。

下载表单时,我想从列表中删除它们。为此,我在updateFormsNotDownloaded使用FormAdapter

    public class FormAdapter extends ArrayAdapter<Form>
    {
        private Context context;
        private int layoutResourceId;
        private ArrayList<Form> forms;
        private ArrayList<Integer> checkedItemsPosition;
        private Button downloadButton;

        public ArrayList<Integer> getCheckedItemsPosition()
        {
            return checkedItemsPosition;
        }

        public String[] getSelectedFormsId()
        {
            String[] ids = new String[checkedItemsPosition.size()];
            int i = 0;
            for(Integer pos : checkedItemsPosition)
            {
                Form f = forms.get(pos.intValue());
                ids[i] = f.FormId;
                i++;
            }
            return ids;
        }

        /**
         * Called when selected forms has been downloaded and save it locally correctly.
         */
        public void updateFormsNotDownloaded()
        {
            for(Integer pos: checkedItemsPosition)
                remove(forms.get(pos.intValue()));
            checkedItemsPosition.clear();
            notifyDataSetChanged();
        }

        public FormAdapter(Context context, int textViewResourceId,
                ArrayList<Form> objects, Button downloadButton)
        {
            super(context, textViewResourceId, objects);

            this.context = context;
            this.layoutResourceId = textViewResourceId;
            this.forms = objects;
            this.checkedItemsPosition = new ArrayList<Integer>();
            this.downloadButton = downloadButton;
        }

        @Override
        public int getCount()
        {
            return forms.size();
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            Log.v("FormAdapter", "getView.postion: " + position);
            View row = convertView;
            if (row == null)
            {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);
            }

            Form f = forms.get(position);
            if (f != null)
            {
                CheckBox checkBox = (CheckBox)row.findViewById(R.id.itemCheckBox);
                if (checkBox != null)
                {
                    checkBox.setText(f.Name);
                    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
                    {
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked)
                        {
                            //Form f = forms.get(position);
                            if (isChecked)
                            {
                                //checkedItems.add(f.FormId);
                                checkedItemsPosition.add(new Integer(position));
                            }
                            else
                            {
                                //checkedItems.remove(checkedItems.indexOf(f.FormId));
                                int index = checkedItemsPosition.indexOf(new Integer(position));
                                if (index > -1)
                                    checkedItemsPosition.remove(index);
                            }
                            downloadButton.setEnabled(checkedItemsPosition.size() > 0);
                        }
                    });
                }
            }

            return row;
        }
    }
}

想象一下,我有以下列表:

  1. 表格1.(已检查)
  2. 表格2。
  3. 表格3。
  4. 用户选择表单1,当表单保存在本地,并且updateFormsNotDownloaded被调用时,我看到了:

    1. 表格2.(已选中)
    2. 表格3。
    3. 为什么要检查表单2?我怎样才能取消选中所有内容?

2 个答案:

答案 0 :(得分:1)

您应该检查是否应该检查该位置并手动设置每个位置的选中值。我没有看到你在做什么..

实施例

//we set the OnCheckedChangeListener to null so that setting the checkbox value doesn't trigger the checkedChangelistener
//this may or may not be relevant for your project
checkBox.setOnCheckedChangeListener(null); 
checkbox.setChecked(checkedItemsPosition.contains(position));
checkBox.setText(f.Name);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
      {
        //the rest of your code here
      }
}

答案 1 :(得分:1)

使用CheckBox检查GridView的此链接.. Sample for GridView with Image and checkbox