Custom Listview with CheckBox single selection

后端 未结 2 490
臣服心动
臣服心动 2020-12-11 10:17

Here I am creating custom listview with checkbox / RadioButton. I got this but I need the single selection for that.

I try using this lstvw.setChoiceMode(ListV

2条回答
  •  眼角桃花
    2020-12-11 10:28

    You can use CheckBox in your adapter class. Try this.

       @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
    
            if (convertView == null) {
                inflater = LayoutInflater.from(adapterContext);
                convertView = inflater.inflate(R.layout.view, null);
                final ViewHolder viewHolder = new ViewHolder();
    
                viewHolder.name = (TextView) convertView.findViewById(R.id.txtName);
                viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
    
                convertView.setTag(viewHolder);
            }
    
            final ViewHolder holder = (ViewHolder) convertView.getTag();
    
            holder.name.setText(collectContactList.get(position).getName());
    
            holder.checkBox.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    CheckBox cb = (CheckBox) v;
                    if (cb.isChecked() == true) 
                    {
                        // Here you will get list of checked item.
                    }
    
                    else 
                   {
                    // Here you will get list of unchecked item.    
                    }
    
                }
            });
    

    Hope this will help you.

提交回复
热议问题