Finding the Checked state of checkbox in a custom listview
Hai我正在尝试开发一个应用程序,通过该应用程序,我可以向特定人群发送短信和电子邮件。
我有一个列表视图,显示了我组中的联系人。每一行的格式为
TextView(名称)TextView(电话)复选框(sms)
TextView(电子邮件ID)复选框(邮件)
我已使用自定义适配器将联系人详细信息显示到listview。我已设置onitemclick侦听器以查找行的位置。
我必须将短信和电子邮件发送到已将其复选框设置为true的那些联系人。我如何才能找到每个复选框的状态。
请提前帮助我。
我已在我创建的自定义adapetr下添加了..
公共类ContactInfoAdapter扩展了ArrayAdapter {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | private ArrayList<Boolean> mChecked_sms,mChecked_email; Context context; int layoutResourceId; ContactInfo data[] = null; public ContactInfoAdapter(Context context, int layoutResourceId, ContactInfo[] data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; mChecked_sms = new ArrayList<Boolean>(); mChecked_email = new ArrayList<Boolean>(); for (int i = 0; i < this.getCount(); i++) { mChecked_sms.add(i, false); mChecked_email.add(i,false); } } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ContactHolder holder; View row = convertView; if(row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new ContactHolder(); holder.txtName = (TextView)row.findViewById(R.id.textViewName); holder.txtPhone = (TextView) row.findViewById(R.id.textViewPhone); holder.txtEmail = (TextView) row.findViewById(R.id.textViewEmail); holder.cb_sms_state = (CheckBox) row.findViewById(R.id.checkBox1); holder.cb_email_state = (CheckBox) row.findViewById(R.id.checkBox2); row.setTag(holder); } else { holder = (ContactHolder)row.getTag(); } ContactInfo contact = data[position]; holder.txtName.setText(contact.name); holder.cb_sms_state.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (holder.cb_sms_state.isChecked()) { mChecked_sms.set(position, true); Toast.makeText(getContext(),"checked", 2).show(); } else { mChecked_sms.set(position, false); } } }); holder.cb_sms_state.setChecked(mChecked_sms.get(position)); holder.cb_email_state.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (holder.cb_email_state.isChecked()) { mChecked_email.set(position, true); Toast.makeText(getContext(),"checked", 2).show(); } else { mChecked_email.set(position, false); } } }); holder.cb_email_state.setChecked(mChecked_email.get(position)); holder.txtPhone.setText(contact.number); holder.txtEmail.setText(contact.email); return row; } static class ContactHolder { TextView txtName; TextView txtPhone; TextView txtEmail; CheckBox cb_sms_state; CheckBox cb_email_state; } |
} ??
ContactInfo类为:
公共类ContactInfo {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | public String name; public String number; public String email; public boolean sms_state; public boolean email_state; public ContactInfo(){ super(); } public ContactInfo(String name,String number,String email,boolean sms_state,boolean email_state) { super(); this.name = name; this.number = number; this.email = email; this.sms_state = sms_state; this.email_state = email_state; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setNUmber(String number) { this.number = number; } public String getNumber() { return number; } public void setEmail(String email) { this.email = email; } public String getEmail() { return email; } public void setSms_state(Boolean sms_state) { this.sms_state = sms_state; } public Boolean getSms_state(){ return sms_state; } public void setEmail_state(Boolean email_state) { this.email_state = email_state; } public Boolean getEmail_state(){ return email_state; } |
在
这是一个侦听器代码,例如:
1 2 3 4 5 6 7 8 9 10 11 | ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if ( isChecked ) { // perform logic } } }); |
由于ListView视图被回收,因此您不能依靠侦听特定实例,而必须在数据模型中存储"已检查"状态。您还将需要自定义列表适配器,您可以在其中创建和填充各个条目。必须执行以下操作:
-在重写的getView()中,创建新视图(如果未提供convertView)或膨胀新视图
-从您的数据模型填充viewv字段
-删除旧的onclick侦听器,并设置一个新的(可以是匿名内部类)来修改您的数据模型
PS:如果您的列表很大,则回收视图非常重要
我的应用程序Hasta La Vista遇到了这个问题,我创建了一个带有自定义列表视图的已选中项目,并且我需要获取已选中项目,这是解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ListView lv = getListView(); //ver listview if(lv != null){ final SparseBooleanArray checkedItems = lv.getCheckedItemPositions(); //get checked items if (checkedItems == null) { return; } final int checkedItemsCount = checkedItems.size(); for (int i = 0; i < checkedItemsCount; ++i) { // This tells us the item position we are looking at final int position = checkedItems.keyAt(i); // This tells us the item status at the above position final boolean isChecked = checkedItems.valueAt(i); if(isChecked){ //get item from list and do something lv.getAdapter().getItem(position); } } } |
这是一个简单的示例:
1 2 3 4 5 6 7 8 9 10 | mContactListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView< ? > parent, View view, int position, long arg3) { CheckBox chkContact = (CheckBox) view.findViewById(R.id.listrow_contact_chkContact); if (chkContact.isChecked()) { ... } }); |
参阅文档。
我认为您需要:
1 | checkbox.isChecked() |