【问题标题】:how to add one checkbox in a listview in android?如何在android的列表视图中添加一个复选框?
【发布时间】:2013-09-01 11:59:16
【问题描述】:

我只想在列表视图中添加一个复选框,我正在研究并发现如何在所有行中使用复选框,但我只希望它在一行中。

提前致谢

--编辑--

谢谢@Gaurav,我正在研究,我可以做到,但现在当我尝试点击带有复选框的项目时,它没有被点击

基础适配器

public View getView(int arg0, View arg1, ViewGroup arg2) {
    View v = arg1;
    if(arg1 == null){
        LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inf.inflate(R.layout.row, null);
    }
    Articulos art = items.get(arg0);
    TextView nombre = (TextView) v.findViewById(R.id.tvNombreRow);
    nombre.setText(art.getNombre());
    CheckBox check = (CheckBox) v.findViewById(R.id.chkCuadroRow);
    check.setChecked(art.getEstado());
    if(art.getVisible()){
         check.setVisibility(View.VISIBLE);
    }else{
        check.setVisibility(View.INVISIBLE);
    }
    return v;
}

主要

ArrayList<Articulos> arrayList = new ArrayList<Articulos>();
    Articulos articulos;
    articulos = new Articulos("Color de fondo",false,false);
    arrayList.add(articulos);
    articulos = new Articulos("Vencimiento de bonos",false,false);
    arrayList.add(articulos);
    articulos = new Articulos("Comparte esta aplicacion",false,false);
    arrayList.add(articulos);
    articulos = new Articulos("Visualizador de tiempo",true,true);
    arrayList.add(articulos);
    articulos = new Articulos("Configuración",false,false);
    arrayList.add(articulos);
    articulos = new Articulos("Tutorial",false,false);
    arrayList.add(articulos);
    articulos = new Articulos("Acerca de",false,false);
    arrayList.add(articulos);
    articulos = new Articulos("Salir",false,false);
    arrayList.add(articulos);

    BaseAdapterCustom adapter = new BaseAdapterCustom(this, arrayList);
    lstConfiguracion.setAdapter(adapter);

可能是什么问题?

【问题讨论】:

  • 你需要的是一个listViewAdapter。在它的 getView 中,让你的测试知道你想要膨胀哪个视图。
  • 请详细说明你想要什么
  • @DamienR。是当前位置的基础,我们可以做到这一点
  • @Gaurav 例如:我想要一个包含 10 个或更多项目的列表视图,但在第 5 个位置它需要有一个复选框,只有那个项目。
  • 添加了一个答案你得到了没有。或者你到目前为止做了什么发布代码,以便我们可以更正确地帮助你

标签: android


【解决方案1】:

在 getView 的 BaseAdapater 类中你可以试试这个

public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        vi = inflater.inflate(R.layout.your_layout, null);
         if(position = yourcondtion){
            //set the visibility of the checkbox true or false
         } 
            return vi;
        } 

勾选复选框试试这个

 CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox);
        checkBox.setChecked(!checkBox.isChecked());

【讨论】:

  • 这个会在不勾选的时候勾选
  • 这不是问题,我可以设置复选框是否选中,但是当我尝试用复选框触摸行时它不会点击。
  • 我没有得到你想问我的问题,请解释一下,以便我可以帮助你,或者如果你可以给我发邮件
  • 在这个first picture 中,我点击的行显然是蓝色的,但在second 中,我尝试点击它,但它没有点击。
  • 看到这可能对你有帮助stackoverflow.com/questions/12051813/…
猜你喜欢
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多