-1

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;
}
Deepak
  • 54
  • 2
  • 10

5 Answers5

1

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
        }

    }
});
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • true! but I think there should be a design pattern where if list row has a checkbox than checkbox state should be changed on click on the whole list row, not only checkbox itself. – Oleksii Malovanyi Oct 31 '11 at 12:42
  • I did implement this..but i need to know how i can retrieve the arraylist in which i storee these values to the activity page. – Deepak Nov 01 '11 at 09:44
0

See the doc.

I think you need :


checkbox.isChecked()
Michaël
  • 3,679
  • 7
  • 39
  • 64
0

Here's a simple sample:

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()) {
                ...
            }
});
Oleksii Malovanyi
  • 6,780
  • 6
  • 24
  • 27
  • note, this will work for clicking on whole list only if you set right parameters for your checkbox: – Oleksii Malovanyi Oct 31 '11 at 12:40
  • Yes..i hv set the checkbox focusable as false and the listview focusable as true..i need to know how i can reteieve the values in the arraylist to which i store the states of the checkboxes. – Deepak Nov 01 '11 at 09:46
  • why don't you create new List readyForSendingContacts and List.add inside if (chkContact.isChecked()){...}? This is also effective when you give user a confirmation check. Otherwise you could add boolean isChecked to your Contact POJO but IMHO it's unnecessary. Also rememer you can't rely on ChkBx.isChecked while it's inside of (Array?)Adapter and ChkBx is not relying on Contact POJO... – Oleksii Malovanyi Nov 01 '11 at 12:20
0

I've had this problem with my app Hasta La Vista I've create a with custom listview with checked items and I needed to get checked items this was the solution:

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

            }
        }

    }
Pedro Rainho
  • 4,234
  • 1
  • 19
  • 21
0

As ListView views are recycled, you can not rely on listening on particular instance, you will have to store "checked" state in your data model. You will also need custom list adapter, where you create and populate individual entries. Following shall be done: - in overriden getView(), either create new view ( in case no convertView was supplied ) or inflate new one - populate viewv fields from you data model - remove old onclick listener, and set new one ( can be anonymous inner class ) modifying your data model

PS: recycling views is important if your list is big

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • I have a custom apadter for populating the listview..and in the getVIew() i hv used a arraylist to store the boolean of the checkboxes by setting a checkedchange listener.how can i retrieve this array to the activity. – Deepak Nov 01 '11 at 09:43
  • I have no idea, as I did not seen your code. Post it, then somebody may help you – Konstantin Pribluda Nov 01 '11 at 09:46