如何做到把一个CheckBox在ListView? [英] How to do put a CheckBox in a ListView?

查看:307
本文介绍了如何做到把一个CheckBox在ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个custon适配器列表视图。我行的布局,我有一个文本和一个复选框。
当我加载列表视图,我从一个数据库中的数据,它有一个colunm决定是否行cheched与否​​。当我打开列表,它的确定,是要保持检查的行,保持checkd,和其他​​人没有。现在的问题是:当我unckheck一排答滚动列表向下和向上,当我回到起点,我有不加以控制,收益再次检查该行,我怎么能转卖此问题:

在getView()code如下:

 公共查看getView(INT索引,视图观点,父母的ViewGroup){    如果(查看== NULL){
        LayoutInflater充气= LayoutInflater.from(parent.getContext());
        鉴于= inflater.inflate(R.layout.linha_acessorios,父母,假);
    }    最后AcessoriosItensLista acessorios =(AcessoriosItensLista)的getItem(指数);    最后ImageView的imgAcessorio =(ImageView的)view.findViewById(R.id.imgAcessorioLista);    最后复选框CB =(复选框)view.findViewById(R.id.cbListaAcessorios);
    TextView的tvNome =(TextView中)view.findViewById(R.id.tvNomeAcessoriosLinha);
    tvNome.setText(acessorios.getNomeAcessorio());    最后整数iditem = Integer.valueOf(acessorios.getId());    布尔CH = acessorios.isChecked();    最后整数位置= Integer.valueOf(指数);    如果(CH){
        如果(!checked.contains(iditem)){
            checkedPositions.add(位置);
            checked.add(iditem);
        }
    }    cb.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            如果(checked.contains(iditem)){
                checked.remove(iditem);
                checkedPositions.remove(位置);
            }            如果(((复选框)ⅴ).isChecked()){
                checkedPositions.add(位置);
                checked.add(iditem);
                INT ID = context.getResources()则getIdentifier(acc_gold _+ acessorios.getId(),可拉伸,context.getPackageName())。
                imgAcessorio.setBackgroundResource(ID);
            }
            否则,如果(checkedPositions.contains(位置)){
                checkedPositions.remove(位置);
                checked.remove(iditem);
                INT ID = context.getResources()则getIdentifier(ACC _+ acessorios.getId(),绘制,context.getPackageName());
                imgAcessorio.setBackgroundResource(ID);
            }
        }
    });    如果(checkedPositions.contains(位置)){
        cb.setChecked(真);
        INT ID = context.getResources()则getIdentifier(acc_gold _+ acessorios.getId(),可拉伸,context.getPackageName())。
        imgAcessorio.setBackgroundResource(ID);
    }其他{
        cb.setChecked(假);
        INT ID = context.getResources()则getIdentifier(ACC _+ acessorios.getId(),绘制,context.getPackageName());
        imgAcessorio.setBackgroundResource(ID);
    }    返回视图。
}


解决方案

我的猜测是,也许你正在取消选中了复选框但你在任何地方保存其状态,所以当该行通过滚动从屏幕上消失,并再次向下滚动,它再次从数据库加载数据,并在其中的检查。我不知道你是如何处理你的 ArrayAdapter 扩展,但我建议保存构造的的ArrayList 为实例在类中,取消选中在更新它内部的价值和呼叫 notifyDataSetChanged()

---- ----编辑

要保存的ArrayList 类里面,你必须创建一个单独的类(你工作的两个字段),例如:

 类MyRow {
  复选框CB;
  TextView的电视;
}

所以,当你在活动宣布自定义适配器,您就必须申报pviously $ P $有一些初步的元素一个ArrayList(甚至是空的):

 的ArrayList< MyRow> myList中=新的ArrayList< MyRow>();MyRow ROW1 =新MyRow();
row1.cb.isChecked(...);
row1.tv.setText(...);
myList.add(ROW1);

然后调用适配器类的构造函数,像这样:

MyArrayAdapter适配器=新MyArrayAdapter(背景下,R.layout.your_layout,myList中);

所以,当你把它传递给您的适配器类的构造函数,你保存它的一个副本中的那个类:

 公共类MyArrayAdapter扩展ArrayAdapter {
  最终私人的ArrayList< MyRow>为myContent;
  ...  MyArrayAdapter(上下文的背景下,INT my_layout,ArrayList的< MyRow> myContent_){
    ...
    为myContent = myContent_
  }
}

所以,现在,你改变的任何内容(例如像选中/取消选中复选框),你必须保存为myContent阵列中的状态。你会找到的getItem该项目(位置) getView()方法,让你需要的变化。之后,你只需要调用 notifyDataSetChanged(); 方法,它会自动显示在改变你的的ListView

I have a listview with a custon adapter. I the row's layout, I have a text and a checkbox. When I load the listview, I get the data from a database and it has one colunm that determine if the row is cheched or not. When I load the list, its ok, the rows that has to stay checked, stays checkd, and the others no. The problem is: when I unckheck a row ans roll the list down and up, when I return to the start, the row that I had unchecked, returns checked again, how can I resold this problem:

The getView() code below:

public View getView(int index, View view, ViewGroup parent) {

    if (view == null) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.linha_acessorios, parent, false);             
    }       

    final AcessoriosItensLista acessorios = (AcessoriosItensLista)getItem(index);

    final ImageView imgAcessorio = (ImageView)view.findViewById(R.id.imgAcessorioLista);            

    final CheckBox cb = (CheckBox)view.findViewById(R.id.cbListaAcessorios);
    TextView tvNome = (TextView) view.findViewById(R.id.tvNomeAcessoriosLinha);
    tvNome.setText(acessorios.getNomeAcessorio());      

    final Integer iditem = Integer.valueOf(acessorios.getId());

    boolean ch = acessorios.isChecked();

    final Integer position = Integer.valueOf(index);

    if(ch){
        if(!checked.contains(iditem)){
            checkedPositions.add(position);
            checked.add(iditem);
        }
    }               

    cb.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {
            if(checked.contains(iditem)){   
                checked.remove(iditem);
                checkedPositions.remove(position);
            }

            if (((CheckBox) v).isChecked()) {                  
                checkedPositions.add(position);       
                checked.add(iditem);
                int id = context.getResources().getIdentifier("acc_gold_"+acessorios.getId(), "drawable", context.getPackageName());
                imgAcessorio.setBackgroundResource(id);                      
            }
            else if(checkedPositions.contains(position)) {
                checkedPositions.remove(position);  
                checked.remove(iditem);
                int id = context.getResources().getIdentifier("acc_"+acessorios.getId(), "drawable", context.getPackageName());
                imgAcessorio.setBackgroundResource(id);                     
            }                   
        }
    }); 

    if(checkedPositions.contains(position)){
        cb.setChecked(true);    
        int id = context.getResources().getIdentifier("acc_gold_"+acessorios.getId(), "drawable", context.getPackageName());
        imgAcessorio.setBackgroundResource(id);                         
    } else {
        cb.setChecked(false);
        int id = context.getResources().getIdentifier("acc_"+acessorios.getId(), "drawable", context.getPackageName());
        imgAcessorio.setBackgroundResource(id);             
    }               

    return view;
}

解决方案

My guess is that probably you're unchecking that CheckBox but you're not saving its status anywhere, so when that row disappears from the screen by scrolling and you scroll down again, it loads the data again from the database and it's checked in it. I don't know how you're handling your ArrayAdapter extension, but I recommend saving the constructor's ArrayList as an instance inside the class, updating that value inside of it on uncheck, and call notifyDataSetChanged().

---- EDIT ----

To store the ArrayList inside your class, you'll have to create a separate class (with the two fields you're working on), for example:

class MyRow {
  CheckBox cb;
  TextView tv;
}

So when you declare your custom adapter in your Activity, you'll have to declare previously an ArrayList with some initial elements (or even empty):

ArrayList<MyRow> myList = new ArrayList<MyRow>();

MyRow row1 = new MyRow();
row1.cb.isChecked(...);
row1.tv.setText(...);
myList.add(row1);

Then you call the constructor of your adapter class, something like this:

MyArrayAdapter adapter = new MyArrayAdapter(context, R.layout.your_layout, myList);

So when you pass it to the constructor of your adapter class, you save a copy of it in that class:

public class MyArrayAdapter extends ArrayAdapter {
  final private ArrayList<MyRow> myContent;
  ...

  MyArrayAdapter(Context context, int my_layout, ArrayList<MyRow> myContent_) {
    ...
    myContent = myContent_
  }
}

So now, any content you change (like for example checking/unchecking a checkbox) you have to save its state in the myContent array. You would find that item by getItem(position) in your getView() method and make the changes you need. After it, you just have to call the notifyDataSetChanged(); method and it will automatically display the changes in your ListView.

这篇关于如何做到把一个CheckBox在ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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