如何限制复选框选择在列表视图? [英] how to limit checkbox selection in listview?

查看:95
本文介绍了如何限制复选框选择在列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我想限制复选框选择在Android的listivew到例如只有3复选框应选择否则就应该给错误消息。

i want to limit checkbox selection in android listivew to for example only 3 checkboxes should be selected otherwise it should give error message.

用户可以从列表中选择任何三个复选框 任何一个指导我如何实现这一目标?这里是我的适配器

user can select any three checkboxes from the list any one guide me how to achieve this? here is my adapter

public class AdapterContacts extends BaseAdapter  {

    private LayoutInflater mInflater;

    public Context context;
    public static List<myContacts> contacts;

    public AdapterContacts(Context context,List<myContacts> list) {
      mInflater = LayoutInflater.from(context);
      this.context = context;
      contacts= list;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

      ViewHolder holder;

      if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_contacts, null);
        holder = new ViewHolder();
        holder.contactName = (TextView) convertView.findViewById(R.id.contactName);
        holder.contactNumber = (TextView) convertView.findViewById(R.id.contactNumber);
        holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
        convertView.setTag(holder);

      } else {
        holder = (ViewHolder) convertView.getTag();
      }

      myContacts contact = getItem(position);
      holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) {
                 myContacts c = (myContacts) checkboxView.getTag();
                 c.setSelected(isChecked);
                 // to put that check of selection limit with error


            }
        });


      holder.checkBox.setTag(contact);
      holder.checkBox.setChecked(contact.isSelected());



      holder.contactName.setText(contact.getContactName());
      holder.contactNumber.setText(contact.getPhoneNumber());

        return convertView;

        }

    @Override
    public int getCount() {
        return contacts.size();
    }

    @Override
    public myContacts getItem(int position) {
        return contacts.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }


    class ViewHolder {
        TextView contactName;
        TextView contactNumber;
        CheckBox  checkBox;
      }




}

任何帮助将是AP preciated。

any help would be appreciated.

推荐答案

我终于解决了这个问题:)在这里为globalInc是全局变量,默认值为0

finally i solved this issue :) where as globalInc is global variable with default value 0

holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() 
      {

            @Override
            public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) 
            {
                 myContacts c = (myContacts) checkboxView.getTag();

                 if(isChecked)
                 {
                     globalInc++;
                 }
                 else if(!isChecked)
                 {
                     globalInc--;
                 }
                 if(globalInc >= 4)// it will allow 3 checkboxes only
                 {
                     Toast.makeText(context, "Error = " + globalInc, Toast.LENGTH_LONG).show();
                     checkboxView.setChecked(false);
                     globalInc--;
                 }
                 else
                 {
                    c.setSelected(isChecked);
                 }
                 System.out.println(" ---------------    "+globalInc);
            }
        });

这篇关于如何限制复选框选择在列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆