ListView.getCheckedItemPositions无法返回已检查项SparseBooleanArray
问题描述:
我尝试读取多个checkboxes
列表中的联系人,但是当我调用sparsebooleanarray
..它只是返回false为所有列表条目,将在S检查......我看着这个线程Why is ListView.getCheckedItemPositions() not returning correct values? ......但是,当我实现addClickHandlerToCheckBox它迫使stops..this已被窃听我4 days..please任何帮助..ListView.getCheckedItemPositions无法返回已检查项SparseBooleanArray
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);
}
}
}
});
}
答
我认为你应该使用列表适配器视图。
它会给onCheckedChanged监听器,所以你也会得到这个位置。
像...
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int cupos = position+1;
}
});
现在来看整个例如:
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 ;
}
}
试试这个 - 它会帮助你得到的位置和检查是否真或假。