列表视图+复选框

时间:2014-07-29 10:32:42

标签: android listview checkbox onclicklistener

我正在尝试使用复选框执行列表视图,但我无法执行此操作。 我不知道如何实现监听器,告诉我是否选择了ckeckbox。

我留下我的代码,如果有人知道如何实现监听器并且可以帮助我。

这是列表中每个项目的代码:

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

这是适配器的代码:

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();

}

}

这是活动的代码:

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

}

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>

2 个答案:

答案 0 :(得分:1)

查看本教程,它解释了listView Tutorial

中的持有人模式

还记得你需要在你的适配器的getView()方法中分配任何onClickListener或onCheckListener

答案 1 :(得分:1)

getView(...)中,您必须使用CompoundButton.OnCheckedChangeListener尝试使用ViewHolder pattern

代码段

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());

            }
          });