自定义列表视图与复选框 - 滚动检查状态互换

时间:2011-04-04 13:05:29

标签: android

我使用自定义列表视图和复选框。滚动列表视图时,检查状态互换如何控制互换并设置默认值,即我选中的选中项目

代码供您参考

public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) 
        {
            convertView=lInflater.inflate(R.layout.subtag, null);
            holder=new viewHolder();
            holder.check=(CheckBox)convertView.findViewById(R.id.subtag_checkCatagory);
            holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    if(isChecked)
                    {
                        System.out.println("checked item:"+buttonView.getText());
                        temp.add(buttonView.getText().toString());
                    }
                    else
                    {
                        temp.remove(buttonView.getText().toString());

                    }
                }
            });
            convertView.setTag(holder);
        }
        else
        {   
            holder = (viewHolder) convertView.getTag();
        }


        holder.check.setText(list.get(position));

        return convertView;
    }

1 个答案:

答案 0 :(得分:4)

最后我找到了上述问题的答案。在此代码中,当我们滚动TextView

时,检查值和ListView值不会发生变化

步骤

  1. 而非BaseAdapter使用ArrayAdapter代替 customlistview
  2. 为选中/未选中的复选框和textview get / set
  3. 编写bojo类
  4. 按照下面的规定,专注于getview()
  5. <强>代码

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
    
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.subtag, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.subtag_textCatagory);
            viewHolder.check = (CheckBox) view.findViewById(R.id.subtag_checkCatagory);
    
            viewHolder.text.setOnClickListener(this);
            viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                            TagBojo element = (TagBojo) viewHolder.check.getTag();
                            element.setSelected(buttonView.isChecked());
    
                            System.out.println(element.getListName());
                    });
            view.setTag(viewHolder);
            viewHolder.check.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).check.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getListName());
        holder.check.setChecked(list.get(position).getSelected());
        return view;
    }