Customlistview with checkbox- while scrolling check state is interchanged

后端 未结 1 1230
感动是毒
感动是毒 2021-01-07 04:35

I use customize listview with checkbox.while scrolling the listview the checked state is interchanged how to control interchanged and set it default ie checked items i sele

相关标签:
1条回答
  • 2021-01-07 04:53

    At last I found answer to above question. In this code check value and TextView value is not changing while we scroll the ListView

    Steps

    1. Instead of BaseAdapter use ArrayAdapter for customlistview
    2. Write a bojo class for checkbox selected/unselected and textview get/set
    3. Concentrate on getview() as I prescribed below

    Code

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
    
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.subtag, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.subtag_textCatagory);
            viewHolder.check = (CheckBox) view.findViewById(R.id.subtag_checkCatagory);
    
            viewHolder.text.setOnClickListener(this);
            viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                            TagBojo element = (TagBojo) viewHolder.check.getTag();
                            element.setSelected(buttonView.isChecked());
    
                            System.out.println(element.getListName());
                    });
            view.setTag(viewHolder);
            viewHolder.check.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).check.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getListName());
        holder.check.setChecked(list.get(position).getSelected());
        return view;
    }
    
    0 讨论(0)
提交回复
热议问题