ListView控件使用复选框和点击监听器 [英] ListView with CheckBoxes and a click listener

查看:125
本文介绍了ListView控件使用复选框和点击监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个点击侦听器使用自定义适配器ListView的复选框。像这样的:

I would like to implement a click listener for the CheckBoxes of a ListView that uses a custom adapter. Like this:

我了解choiceMode的存在,但我想实现我自己。

I know about the existence of the choiceMode, but I would like to implement myself.

我想申请好的做法。 这是我的适配器是这样的:

I'm trying to apply good practices. This is how my adapter looks like:

public class TaskListAdapter extends ArrayAdapter<ListTask> {
    private ArrayList<ListTask> mItems;
    private ArrayList<Boolean> mChecked;
    private LayoutInflater mInflater;

    public TaskListAdapter(Context context, int textViewResourceId, ArrayList<ListTask> items) {
        super(context, textViewResourceId, items);
        this.mItems = items;

        // Cache the LayoutInflate to avoid asking for a new one each time
        mInflater = LayoutInflater.from(context);

    // Initialize all checkboxes with false value
        mChecked = new ArrayList<Boolean>();
        for (int i = 0; i < this.getCount(); i++) {
        mChecked.add(i, false);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_row, null);

            holder = new ViewHolder();
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.mycheckbox);
            holder.text = (TextView) convertView.findViewById(R.id.mytext);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MyList mylist = mItems.get(position);

    holder.text.setText(mylist.getMyText());

        // Create a listener for the CheckBox
        holder.checkbox.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
            if (holder.checkbox.isChecked()) {
            mChecked.set(position, true);
            } else {
            mChecked.set(position, false);
            }
        }

        });
        holder.checkbox.setChecked(mChecked.get(position));

        return convertView;
    }

    static class ViewHolder {
        CheckBox checkbox;
        TextView text;
    }
}

当我跑我的应用程序,我得到这个错误:

When I run my app, I get this error:

java.lang.IndexOutOfBoundsException:   无效的位置0,大小为0

java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0

这错误是由行抛出

holder.checkbox.setChecked(mChecked.get(position));

这仅仅是一个return语句之前。

that is just before the return statement.

如果我评论它,并重新运行应用程序,它显示的列表中,但是当我点击一个复选框,它也抛出一个IndexOutOfBoundsException异常这一行:

If I comment it and re-run the app, it shows the list, but when I click on a checkbox it throws also a IndexOutOfBoundsException for this line:

mChecked.set(position, true);

这是if语句来检查,如果该复选框被选中里面。

that is inside the "if" statement that checks if the checkbox is checked.

我是什么做错了吗?

推荐答案

你检查的位置的值?同时也可以确保你的目标正确的意见,甚至是填充你的名单?机会是你没有填充您的ListView。从我记得地位没有变我感兴趣的是,当它来到ListView和复选框。

Did you check the value of position? Also did you ensure that your targeting the right views and even populating your list? Chances are you didn't populate your ListView. From what I remember position was NOT the variable I was interested in when it came to the ListView and CheckBoxes.

这篇关于ListView控件使用复选框和点击监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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