簡體   English   中英

在自定義列表視圖中使用的帶有Android中RadioButton的列表視圖復選框?

[英]Listview Checkbox with RadioButton in android used in custom listview?

嗨,我正在創建一個帶有動態復選框的自定義列表視圖,添加了我想要的東西,但是當我嘗試選擇第一行中的一個復選框,然后自動選擇了第5、9、13行中的第一個按鈕時,當我選擇了第二行中的任何按鈕,然后第六,第八,第十二行中的同一按鈕被選中,我在這里做錯了什么,我的適配器類

class listViewEventscustomAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    ViewHolder holder;

    public listViewEventscustomAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        if (arrListViewoption_title != null) {
            return arrListViewoption_title.size();
        } else {
            Toast.makeText(getApplicationContext(), "No item found",
                    Toast.LENGTH_LONG).show();
            return 0;
        }
    }

    public Object getItem(int arg0) {
        return arg0;
    }

    public long getItemId(int arg0) {
        return arg0;
    }

    @SuppressWarnings("unchecked")
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View v = convertView;
        final CheckBox checkitem;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.customlistviewdata, null);
        }
        if (v != null) {
            holder = new ViewHolder();
            holder.txtoptiontitle = (TextView) v
                    .findViewById(R.id.optiontitle);
            holder.txtsubtitle = (TextView) v.findViewById(R.id.subtitle);
            checkitem = (CheckBox) v.findViewById(R.id.checkBox1);
            holder.btn_radio = (RadioButton) v
                    .findViewById(R.id.radio0);

            holder.txtoptiontitle.setText(arrListViewoption_title
                    .get(position));
            holder.txtsubtitle.setText(arrlistViewsub_title.get(position));

            if (value.equals(arrListViewoption_type.get(position)
                    .toString().trim())) {
                checkitem.setVisibility(View.VISIBLE);
                holder.btn_radio.setVisibility(View.GONE);

            }

            else {
                holder.btn_radio.setVisibility(View.VISIBLE);
                checkitem.setVisibility(View.GONE);

            }

            checkitem
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton view,
                                boolean isChecked) {
                            // int getPosition = (Integer) view.getTag();
                            // list.get(getPosition).setSelected(view.isChecked());

                            if (view.isChecked()) {

                                checkboxvalue.add(arrlistViewsub_title
                                        .get(position).toString().trim());
                            } else {
                                System.out
                                        .println("*************unchecked");
                                checkboxvalue.remove(arrlistViewsub_title
                                        .get(position).toString().trim());
                                adapter_list.notifyDataSetChanged();

                            }

                            Log.i(this.toString(),
                                    "Item" + checkboxvalue.size());

                        }
                    });

            holder.btn_radio
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(
                                CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub
                            if (buttonView.isChecked()) {


                                // Action Doing here

                            }
                        }
                    });

            v.setTag(holder);

        }
        return v;

    }

}

使用convertview對性能和快速滾動很有用,但可能會導致這種問題。

您應該在重新使用轉換視圖之前重置所有設置值。 在您的情況下:

boolean wasItemChecked = checkboxvalue.contains(arrlistViewsub_title.get(position).toString().trim());
checkItem.setChecked(wasItemChecked );

如果checkItem狀態已更改,則會根據數據源狀態將其重置為true狀態。

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM