我正在尝试读取包含多个的列表中的联系人checkboxes
,但是当我打电话给sparsebooleanarray
..它只是为所有列表条目返回 false,..即使对于已检查的条目也是如此...我调查了这个线程为什么 ListView.getCheckedItemPositions() 没有返回正确的值? https://stackoverflow.com/questions/3996938/why-is-listview-getcheckeditempositions-not-returning-correct-values...但是当我实现 addClickHandlerToCheckBox 时,它强制停止..这已经困扰我 4 天了..请提供帮助..
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 侦听器,因此您也将获得该位置。
Like...
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 ;
}
}
试试这个——它将帮助您获得位置以及检查是否正确。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)