发现在自定义列表视图复选框的选中状态 [英] Finding the Checked state of checkbox in a custom listview
问题描述
海我试图开发一个应用程序通过在那里我可以发送短信和电子邮件向特定群体。
我有示出在我group.Each排联系人列表视图的形式为
的TextView(名称)的TextView(电话)复选框(SMS)结果
TextView中(电子邮件ID)复选框(邮件)
我已经用自定义适配器显示的联系方式设置onitemclick监听器找到该行的位置listview.ihave ..
我要发送短信和电子邮件,为其作为true.how我能找到的每一个复选框的状态复选框已设置的联系人。
请帮忙me..lotz感谢名单提前..
我甲肝加我HV创建自定义adapetr低于..
公共类ContactInfoAdapter扩展ArrayAdapter {
私人的ArrayList<布尔> mChecked_sms,mChecked_email;上下文语境;
INT layoutResourceId;
数据的ContactInfo [] = NULL;公共ContactInfoAdapter(上下文的背景下,INT layoutResourceId,的ContactInfo []数据){
超级(上下文,layoutResourceId,数据);
this.layoutResourceId = layoutResourceId;
this.context =背景;
this.data =数据;
mChecked_sms =新的ArrayList<布尔>();
mChecked_email =新的ArrayList<布尔>();
的for(int i = 0; I< this.getCount();我++){
mChecked_sms.add(I,FALSE);
mChecked_email.add(I,FALSE);
}}@覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
最后ContactHolder持有人;
查看排= convertView; 如果(行== NULL)
{
LayoutInflater充气=((活动)上下文).getLayoutInflater();
行= inflater.inflate(layoutResourceId,父母,假); 持有人=新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 =(复选框)row.findViewById(R.id.checkBox1);
holder.cb_email_state =(复选框)row.findViewById(R.id.checkBox2);
row.setTag(保持器);
}
其他
{
支架=(ContactHolder)row.getTag();
} 的ContactInfo触点=数据[位置]
holder.txtName.setText(contact.name);
holder.cb_sms_state.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
如果(holder.cb_sms_state.isChecked()){
mChecked_sms.set(位置,真正的);
Toast.makeText(的getContext(),检查,2).show();
}其他{
mChecked_sms.set(位置,FALSE);
}
} });
holder.cb_sms_state.setChecked(mChecked_sms.get(位置)); holder.cb_email_state.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
如果(holder.cb_email_state.isChecked()){
mChecked_email.set(位置,真正的);
Toast.makeText(的getContext(),检查,2).show();
}其他{
mChecked_email.set(位置,FALSE);
}
} });
holder.cb_email_state.setChecked(mChecked_email.get(位置)); holder.txtPhone.setText(contact.number);
holder.txtEmail.setText(contact.email); 返回行;
}
静态类ContactHolder
{ TextView的txtName的;
TextView的txtPhone;
TextView的txtEmail;
复选框cb_sms_state;
复选框cb_email_state;
}
}
本的ContactInfo类是:
公共类的ContactInfo {
公共字符串名称;
公共串号;
公共字符串的电子邮件;
公共布尔sms_state;
公共布尔email_state;
公众的ContactInfo(){
超();
}公众的ContactInfo(字符串名称,串号,串电子邮件,布尔sms_state,布尔email_state){
超(); this.name =名称;
this.number =号;
this.email =电子邮件;
this.sms_state = sms_state;
this.email_state = email_state;
}
公共无效setname可以(字符串名称){
this.name =名称;
}公共字符串的getName(){
返回名称;
}公共无效setNUmber(串号){
this.number =号;
}公共字符串getNumber(){
返回数;
}公共无效setEmail(字符串email){
this.email =电子邮件;
}公共字符串getEmail(){
返回的电子邮件;
}
公共无效setSms_state(布尔sms_state)
{
this.sms_state = sms_state;
}
公共布尔getSms_state(){
返回sms_state;
}
公共无效setEmail_state(布尔email_state)
{
this.email_state = email_state;
}
公共布尔getEmail_state(){
返回email_state;
}
在 getView()
方式,必须实施< STRONG> OnCheckedChangeListener
的复选框。
下面是一个监听器code,例如说:
ChkBx.setOnCheckedChangeListener(新OnCheckedChangeListener()
{
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked)
{
如果(器isChecked)
{
//执行逻辑
} }
});
Hai i'm trying to develop an app where by i can send sms and email to a particular group of people..
I have a listview showing the contacts which are in my group.Each row is of the form
TextView(Name)TextView(phone) Checkbox(sms)
TextView(email id) Checkbox(mail)
I have used custom adapter to display the contact details to the listview.ihave set the onitemclick listener to find the position of the row.. I have to send sms and email to those contacts for which checkboxes have been set as true.how can i find the state of each of the checkboxes.
Please help me..lotz of thanx in advance..
I hav added below the custom adapetr i hv created..
public class ContactInfoAdapter extends ArrayAdapter{
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;
}
}
The ContactInfo class is :
public class ContactInfo {
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;
}
Inside the getView()
method, you have to implement a OnCheckedChangeListener
for the CheckBox.
Here is a listener code, say for example:
ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
这篇关于发现在自定义列表视图复选框的选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!