1

我用一组行做了listview。每行都有一个复选框。如果我单击一个复选框,则会选中另一个复选框。我会做些什么来避免这种情况?谁知道帮帮我..

4

3 回答 3

2

为避免 ListView 中的 CheckBoxes 出现问题,您可以在开始时采用一个初始化为 false 的布尔数组,然后在 ListView 中选中复选框的数组中的相应位置设为 true。当您在应用程序中向前和向后移动或滚动 ListView 时,这不会导致复选框的选中状态出现问题。

下面是如何设置 checkboxstate 是布尔数组:

 holder.checkbox.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        if (((CheckBox) v).isChecked())
                            checkBoxState[pos] = true;
                        else
                            checkBoxState[pos] = false;

                    }
                });

然后这将在您滚动时保持复选框处于选中状态,并且状态不会自动更改:

holder.checkbox.setChecked(checkBoxState[pos]);
于 2013-09-07T12:38:38.403 回答
1

这是我的代码:

 public class Favourites extends Activity {
        ListView list;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listfavourites);
            initcomponents();

            list.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {  
                }
            });
            ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();

            for (int i = 1; i < 20; i++) {
                HashMap<String, String> hmap = new HashMap<String, String>();
                hmap.put("itemname", "ItemName" + i);
                hmap.put("price", "Price");
                alist.add(hmap);

            }

            final CustomListAdapter adapter = new CustomListAdapter(this,
                    R.layout.listitemfavourites, alist);

            list.setAdapter(adapter);

        }

        private void initcomponents() {
            list = (ListView) findViewById(R.id.listfavourites_lst_list);

        }

        class CustomListAdapter extends ArrayAdapter<HashMap<String, String>> {
            Context context;
            boolean[] checkBoxState;
            int textViewResourceId;
            ArrayList<HashMap<String, String>> alist;

            public CustomListAdapter(Context context, int textViewResourceId,
                    ArrayList<HashMap<String, String>> alist) {
                super(context, textViewResourceId);
                this.context = context;
                this.alist = alist;
                this.textViewResourceId = textViewResourceId;
                checkBoxState = new boolean[alist.size()];

            }

            public int getCount() {

                return alist.size();
            }

            public View getView(final int pos, View convertView, ViewGroup parent) {
                Holder holder = null;

                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                convertView = inflater.inflate(R.layout.listitemfavourites, parent,
                        false);
                holder = new Holder();
                holder.checkbox = (CheckBox) convertView
                        .findViewById(R.id.listitemfavourites_chk_checkbox);
                holder.itemname = (TextView) convertView
                        .findViewById(R.id.listitemfavourites_txt_itemname);
                holder.price = (TextView) convertView
                        .findViewById(R.id.listitemfavourites_txt_price);
                holder.lin_background = (LinearLayout) convertView
                        .findViewById(R.id.favourites_lin_top);
                convertView.setTag(holder);

                holder = (Holder) convertView.getTag();

                holder.itemname.setText(alist.get(pos).get("itemname"));
                holder.price.setText(alist.get(pos).get("price"));
                holder.checkbox.setChecked(checkBoxState[pos]);
                if (pos == 0) {
                    holder.lin_background
                            .setBackgroundResource(R.drawable.bg_celltop);
                } else if (pos == (alist.size() - 1) && alist.size() != 1) {

                    holder.lin_background
                            .setBackgroundResource(R.drawable.bg_cellbottom);
                }
                holder.checkbox.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        if (((CheckBox) v).isChecked())
                            checkBoxState[pos] = true;
                        else
                            checkBoxState[pos] = false;

                    }
                });

                return convertView;
            }

            class Holder {
                TextView itemname, price;
                CheckBox checkbox;
                LinearLayout lin_background;
            }
        }    
    }
于 2013-09-11T12:54:49.490 回答
0

这是因为您正在通过条件 if(view==null) remove this 来回收视图。

于 2013-09-07T12:25:06.813 回答