在自定义列表视图中查找复选框的已检查状态

时间:2011-10-31 12:27:17

标签: android listview checkbox

Hai我正在尝试开发一个应用程序,我可以通过短信和电子邮件发送给特定人群..

我有一个列表视图,显示我的组中的联系人。每行的格式为

TextView(名称)TextView(手机)复选框(短信)
TextView(电子邮件ID)复选框(邮件)

我使用自定义适配器向listview.i发送联系人详细信息。设置onitemclick侦听器以查找行的位置。 我必须发送短信和电子邮件给那些复选框已设置为true的联系人。我可以找到每个复选框的状态。

请提前帮助我.. thanx ..

我在下面添加了我创建的自定义adapetr ..

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;
}

}

ContactInfo类是:

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;
}

5 个答案:

答案 0 :(得分:1)

getView() 方法中,您必须为CheckBox实施 OnCheckedChangeListener

这是一个监听器代码,例如:

ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
        }

    }
});

答案 1 :(得分:0)

请参阅doc

我认为你需要:


checkbox.isChecked()

答案 2 :(得分:0)

以下是一个简单的示例:

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()) {
                ...
            }
});

答案 3 :(得分:0)

我的应用程序Hasta La Vista出现了这个问题我创建了一个带有选中项目的自定义列表视图,我需要检查项目这是解决方案:

    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);

            }
        }

    }

答案 4 :(得分:0)

当ListView视图被回收时,您不能依赖于侦听特定实例,您必须在数据模型中存储“已检查”状态。您还需要自定义列表适配器,您可以在其中创建和填充各个条目。以下是:     - 在重写getView()中,创建新视图(如果没有提供convertView)或膨胀新视图     - 从您的数据模型填充viewv字段     - 删除旧的onclick侦听器,并设置新的(可以是匿名内部类)修改数据模型

PS:如果你的名单很大,回收意见很重要