列表视图+复选框 [英] Listview + checkbox

查看:146
本文介绍了列表视图+复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个复选框列表视图,但我不能做到这一点。 我不知道我可以实现监听器,告诉我,如果ckeckbox选择或不选择。

我离开这里我code。如果有人知道是怎么监听器的实现,它可以帮助我。

这是在$ C $下列表中的每个项目:

 公共类TemaRescatado {

    保护字符串TEMA;
    受保护的长ID;
    保护布尔选择= FALSE;

    公共TemaRescatado(字符串tema2,长ID){
        超();
        this.tema = tema2;
        this.id = ID;
    }

    公共TemaRescatado(字符串tema2){
        超();
        this.tema = tema2;
    }

    公共字符串getTema(){
        返回TEMA;
    }

    公共无效setTema(字符串tema1){
        this.tema = tema1;
    }

    众长的getId(){
        返回ID;
    }

    公共无效SETID(长ID){
        this.id = ID;
    }

    公共布尔isSelected(){
          返回所选择的;
    }
     公共无效的setSelected(布尔选择){
          this.selected =选择;
    }
}
 

这是在code适配器:

 公共类AdapterGenerales扩展了BaseAdapter {

保护活动的活动;
受保护的ArrayList< TemaRescatado>项目;

公共AdapterGenerales(){
    // TODO自动生成构造函数存根
}

公共AdapterGenerales(活动活动,ArrayList的< TemaRescatado>项目){
    this.activity =活动;
    this.items =项目;
}


公众诠释getCount将(){
    返回items.size();
}

公共对象的getItem(INT为arg0){
    返回items.get(为arg0);
}


@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    // Generamos UNA convertView POR motivos德eficiencia
    视图V = convertView;

    // Asociamos埃尔布局德拉LISTA阙hemos creado
    如果(convertView == NULL){
        LayoutInflater INF =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        V = inf.inflate(R.layout.item_gen,NULL);
    }

    // Creamos未objeto directivo
    TemaRescatado temaResc = items.get(位置);

    // Rellenamos埃尔列表视图去TEMAS
    TextView的鉴定者=(TextView中)v.findViewById(R.id.textView1);
    iden.setText(temaResc.getTema());

    // Retornamos拉维斯塔
    返回伏;
}

@覆盖
众长getItemId(INT位置){
    返回items.get(位置).getId();

}

}
 

这是$ C $下活动:

 公共类SeleccionTest延伸活动{

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_seleccion_test);

    ListView的LISTA =(ListView控件)findViewById(R.id.listView1);
    最后的ArrayList< TemaRescatado> arraydirGen =新的ArrayList< TemaRescatado>();
    TemaRescatado temaRescGen;

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

    最后AdapterGenerales适配器=新AdapterGenerales(这一点,arraydirGen);
    lista.setAdapter(适配器);


    lista.setOnItemLongClickListener(新AdapterView.OnItemLongClickListener(){
        @覆盖
        公共布尔onItemLongClick(适配器视图<>母公司视图中查看,INT位置,长的id){
            Log.i(Seleccionado  - 监听器,POS:+位置+号:+ ID);
            返回true;
        }
    });

}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.seleccion_test,菜单);
    返回true;
}

}
 

在item_gen.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent>

<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBaseline =@ + ID / checkBox1
    机器人:layout_alignBottom =@ + ID / checkBox1
    机器人:layout_toRightOf =@ + ID / checkBox1
    机器人:文本=@字符串/ vacia/>

<复选框
    机器人:ID =@ + ID / checkBox1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真
    机器人:layout_marginLeft =50dp
    机器人:文本=@字符串/ vacia/>

< / RelativeLayout的>
 

解决方案

getView(...)你必须使用<一个href="http://developer.android.com/reference/android/widget/CompoundButton.OnCheckedChangeListener.html"相对=nofollow> CompoundButton.OnCheckedChangeListener 尝试使用 ViewHolder图案

code段

  viewHolder.checkbox =(复选框)view.findViewById(R.id.checkBox1);
     viewHolder.checkbox.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){

            @覆盖
            公共无效onCheckedChanged(CompoundButton buttonView,
                布尔器isChecked){
              TemaRescatado元=(TemaRescatado)viewHolder.checkbox
                  .getTag();
              element.setSelected(buttonView.isChecked());

            }
          });
 

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>

解决方案

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

            }
          });

这篇关于列表视图+复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆