一次仅选中一个复选框的自定义列表视图 [英] custom listview with only one checkbox is selected one at a time

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

问题描述

我有一个自定义列表视图,每一行都包含一个复选框和文本.现在我想要的是,如果选中了列表视图行中的任何一个复选框,那么如果选中了其他行中的其他复选框,它将自动被选中(即一次只能选中一个复选框).我该怎么做. /p>

到目前为止,我所做的如下:

public class CustomAdapter extends BaseAdapter{

    Context context;
    List<String> items;

     boolean array[];


    public CustomAdapter(Context context, List<String> items) {
    super();
    this.context = context;
    this.items = items;
    array =new boolean[items.size()];
}


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v=convertView;
        final int pos=position;
        if(v==null)
        {
            v=LayoutInflater.from(context).inflate(R.layout.list,null);
        }

        TextView txt1=(TextView) v.findViewById(R.id.textView1);
        final CheckBox chkbox=(CheckBox) v.findViewById(R.id.checkBox1);

        txt1.setText(items.get(position));
        int selectedindexitem=0;
        chkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(chkbox.isChecked())
                {
                    array[pos]=true;


                }else{
                    array[pos]=false;
                }
            }
        }); 
        chkbox.setChecked(array[pos]);





        return v; 
    }




}

In this code i can select multiple checkbox at a time but i need only one checkbox should be checked one at a time.

解决方案

尝试更改所有项目的布尔值false通知适配器后排除选定的项目,并实施 解决方案

Try to change all item boolean value false exclude selected item after notify adapter and also implement ViewHolder design pattern for ListView performance :

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list,null);
            holder.txt1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.chkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txt1.setText(items.get(position));
        holder.chkbox.setChecked(array[position]);
        holder.chkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                for (int i=0;i<array.length;i++){
                    if(i==position){
                        array[i]=true;
                    }else{
                        array[i]=false;
                    }
                }
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    class ViewHolder{
        TextView txt1;
        CheckBox chkbox;
    }

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

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