Question

I'm trying to do a listview with a checkbox but i can't do it. I don't know how i can implement the listener that tell me if the ckeckbox is selected or isn't selected.

I leave here my code for if someone knows how is the implementation of the listener and can help me.

This is the code for each item in the list:

    public class TemaRescatado {

    protected String tema;
    protected long id;
    protected Boolean selected = false;

    public TemaRescatado(String tema2, long id) {
        super();
        this.tema = tema2;
        this.id = id;
    }

    public TemaRescatado(String tema2) {
        super();
        this.tema = tema2;
    }

    public String getTema() {
        return tema;
    }

    public void setTema(String tema1) {
        this.tema = tema1;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public boolean isSelected() {
          return selected;
    }
     public void setSelected(boolean selected) {
          this.selected = selected;
    }
}

This is the code for the adapter:

public class AdapterGenerales extends BaseAdapter {

protected Activity activity;
protected ArrayList<TemaRescatado> items;

public AdapterGenerales() {
    // TODO Auto-generated constructor stub
}

public AdapterGenerales(Activity activity, ArrayList<TemaRescatado> items) {
    this.activity = activity;
    this.items = items;
}


public int getCount() {
    return items.size();
}

public Object getItem(int arg0) {
    return items.get(arg0);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Generamos una convertView por motivos de eficiencia
    View v = convertView;

    //Asociamos el layout de la lista que hemos creado
    if(convertView == null){
        LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inf.inflate(R.layout.item_gen, null);
    }

    // Creamos un objeto directivo
    TemaRescatado temaResc = items.get(position);

    //Rellenamos el listview de temas
    TextView iden = (TextView) v.findViewById(R.id.textView1);
    iden.setText(temaResc.getTema());

    // Retornamos la vista
    return v;
}

@Override
public long getItemId(int position) {
    return items.get(position).getId();

}

}

And this is the code for the activity:

public class SeleccionTest extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seleccion_test);

    ListView lista = (ListView) findViewById(R.id.listView1);
    final ArrayList<TemaRescatado> arraydirGen = new ArrayList<TemaRescatado>();
    TemaRescatado temaRescGen;

    temaRescGen = new TemaRescatado("General1",1);
    arraydirGen.add(temaRescGen);
    temaRescGen = new TemaRescatado("General2",2);
    arraydirGen.add(temaRescGen);
    temaRescGen = new TemaRescatado("General3",3);
    arraydirGen.add(temaRescGen);
    temaRescGen = new TemaRescatado("General4",4);
    arraydirGen.add(temaRescGen);
    temaRescGen = new TemaRescatado("General5",5);
    arraydirGen.add(temaRescGen);
    temaRescGen = new TemaRescatado("General6",6);
    arraydirGen.add(temaRescGen);

    final AdapterGenerales adapter = new AdapterGenerales(this, arraydirGen);
    lista.setAdapter(adapter);


    lista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i("Seleccionado - Listener", "pos: "+position +"  id:"+id);              
            return true;
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.seleccion_test, menu);
    return true;
}

}

the item_gen.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/checkBox1"
    android:layout_alignBottom="@+id/checkBox1"
    android:layout_toRightOf="@+id/checkBox1"
    android:text="@string/vacia" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="50dp"
    android:text="@string/vacia" />

</RelativeLayout>
Was it helpful?

Solution

In getView(...) you have to use CompoundButton.OnCheckedChangeListener try to use ViewHolder pattern

code snippet

viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox1);
     viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
              TemaRescatado element = (TemaRescatado) viewHolder.checkbox
                  .getTag();
              element.setSelected(buttonView.isChecked());

            }
          });

OTHER TIPS

Check this tutorial it explains holder pattern in listView Tutorial

Also remember that you need to assign any onClickListener or onCheckListener in getView() method of your adapter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top