问题描述
我米试图读取与多个复选框
A列表中的联系人,但是当我打电话 sparsebooleanarray
。 。它只是返回false,所有列表条目,..即使是一个S检查......我看着这个线程Why是ListView.getCheckedItemPositions()没有返回正确的价值观? ......但是,当我实现addClickHandlerToCheckBox它迫使stops..this一直缠着我要4 days..please任何帮助。
公共无效populateContactList(){
//构建适配器的联系人条目
最后光标光标= getContacts();
的String []字段=新的String [] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.contact_entry,光标,
领域,新的INT [] {R.id.contactEntryText});
mContactList.setAdapter(适配器);
mContactList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
proceedButton.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
SparseBooleanArray检查= mContactList.getCheckedItemPositions();
INT的len = mContactList.getCount();
的for(int i = 0; I< LEN;我++)
{
如果(checked.get(我)==真)
{
字符串项= mContactList.getAdapter()的getItem(checked.keyAt(I))的toString()。
edt.append(,+项目);
}
}
}
});
}
我认为你应该使用适配器列表视图。
它会给一个onCheckedChanged监听器,所以你也将获得的位置。
像...
holder.checkBox.setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
INT cupos =位置+ 1;
}
});
现在看整个例如:
Listview.setAdapter(新settingsBase(getApplicationContext()));
私有类settingsBase扩展了BaseAdapter {
公共settingsBase(上下文的背景下){
layoutInflater = LayoutInflater.from(上下文);
}
@覆盖
公众诠释getCount将(){
返回listview.length;
}
@覆盖
公共对象的getItem(INT位置){
返回的位置;
}
@覆盖
众长getItemId(INT位置){
返回的位置;
}
@覆盖
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
ViewHolder持有人;
holder.checkBox.setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
INT cupos =位置+ 1;
}
});
返回convertView;
}
类ViewHolder {
TextView的txtSettingname,txtShow;
单选按钮单选按钮;
}
}
试试这个 - 它会帮助你得到的位置,以及是否检查true或false
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.
这篇关于ListView.getCheckedItemPositions无法返回检查项目在SparseBooleanArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!