2011-06-15 14 views
5

Estoy tratando de leer los contactos en una lista con checkboxes múltiple, pero cuando llamo al sparsebooleanarray ... simplemente devuelve falso para todas las entradas de la lista, .. incluso para el s uno comprueba ... miré a este hilo Why is ListView.getCheckedItemPositions() not returning correct values? ... Pero cuando implemente la addClickHandlerToCheckBox que forzar stops..this me ha estado molestando durante 4 days..please cualquier ayuda ..ListView.getCheckedItemPositions no puede devolver elementos marcados en 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); 

       } 


      } 
     } 



    });     
} 

Respuesta

1

I cree que debería usar la vista del adaptador de lista.

Le dará un oyente onCheckedChanged, por lo que también obtendrá el puesto.

Como ...

holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       int cupos = position+1; 

      } 
     });   

Ahora mira todo el ejemplo:

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

probar este - que le ayudará a obtener la posición y si comprobado verdadero o falso.

Cuestiones relacionadas