I m trying to read the contacts in a list with multiple checkboxes
, but when i call the sparsebooleanarray
..it just return false for all the list entries,..even for the one s checked ...I looked into this thread Why is ListView.getCheckedItemPositions() not returning correct values? ...But when i implement the addClickHandlerToCheckBox it force stops..this has been bugging me for 4 days..please any help..
public void populateContactList() {
// Build adapter with contact entries
final Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
mContactList.setAdapter(adapter);
mContactList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
proceedButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SparseBooleanArray checked=mContactList.getCheckedItemPositions();
int len = mContactList.getCount();
for(int i=0;i<len;i++)
{
if(checked.get(i)==true)
{
String item = mContactList.getAdapter().getItem(checked.keyAt(i)).toString();
edt.append(","+item);
}
}
}
});
}
I think you should use List Adapter view.
It will give an onCheckedChanged listener, so you will also get the position.
Like...
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int cupos = position+1;
}
});
Now look the whole example:
Listview.setAdapter(new settingsBase(getApplicationContext()));
private class settingsBase extends BaseAdapter{
public settingsBase(Context context) {
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listview.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder ;
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int cupos = position+1;
}
});
return convertView;
}
class ViewHolder{
TextView txtSettingname , txtShow ;
RadioButton radioButton ;
}
}
Try this--it will help you to get the position and whether checked true or false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With