自定义列表视图与复选框问题

发布于 2024-10-26 08:39:34 字数 310 浏览 3 评论 0原文

我正在尝试创建一个自定义列表,其中包含复选框,允许您从列表中选择多个项目。

带有复选框的列表显示正常,但如果我选中一个复选框,然后向下滚动其他项目,列表也会被选中。

它基本上与此处相同的问题

我知道它有一些东西与 android 回收视图的方式有关,但我不知道如何解决这个问题!有人可以帮我吗???

谢谢——迈克

I'm trying to create a custom list which will have checkboxes that allow you to select several items from the list.

The list with the checkboxes is displayed ok but if i check a checkbox and then scroll other items further down the list are also checked.

Its basically the same problem as here

I understand that it has something to do with the way android recycles the view but I cant see how to fix this! Can somebody help me???

Thanks -- Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

习ぎ惯性依靠 2024-11-02 08:39:35

您需要一个数据结构来跟踪检查了哪些行。这可以像 bool[] 检查一样简单。

getView 中,确保将复选框状态设置为 checked[position] 的内容。您还应该 设置一个 OnCheckedChangedListener< /a> getView 中的复选框,以便它们使用 checked[position] = isChecked 更新您的数据。

是的,ListView 中的行会被回收,因此请确保在离开 getView 之前填充行的所有适当数据。

You need a data structure to keep track of which rows are checked. This could be as simple as a bool[] checked.

In your getView, make sure that you set the checkbox state to the contents of checked[position]. You should also set an OnCheckedChangedListener on your check boxes in getView so that they update your data with checked[position] = isChecked.

Yes, the rows in a ListView are recycled, so make sure to populate all the appropriate data for a row before you leave getView.

沫雨熙 2024-11-02 08:39:35

您可以尝试为复选框实现 OnClickListener 而不是 OnCheckChangedListener。这对我有用。

You could try implementing OnClickListener for checkbox instead of OnCheckChangedListener. It worked for me.

離殇 2024-11-02 08:39:35

创建一个ArrayList。将 OnCheckChangedListener 添加到您的复选框。在更改后的方法中,向 ArrayList 添加或删除列表视图 position

getView 方法中,检查 ArrayList 是否包含当前列表视图位置。如果它包含位置,则将检查设置为 true,否则设置为 false。

每次单击复选框时,都会在 ArrayList 中添加或删除 Integer

Create an ArrayList<Integer>. add a OnCheckChangedListener to your checkbox. Inside the changed method, add or remove the list view position to the ArrayList<Integer>.

in your getView method, check to see if the ArrayList<Integer> contains the current list view position. if it contains the position, set checked to true, otherwise false.

every time you click a checkbox, either add or remove the Integer from the ArrayList.

明天过后 2024-11-02 08:39:35

检查下面的代码 -

public View getView(int position, View convertView, ViewGroup parent){
            View view = convertView;
            ViewHolder holder = new ViewHolder();

            if(view == null){

                view = inflater.inflate(R.layout.list_callcycle_blue, null);                
                holder.llContainer = (LinearLayout) view.findViewById(R.id.ll_container);
                holder.lblLabel = (TextView) view.findViewById(R.id.txt_desc);
                holder.cb = (CheckBox) view.findViewById(R.id.cb_store);

                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            final Object data = getItem(position);
            holder.lblLabel.setText(data.getDescription());

            holder.cb.setTag(position);
            holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    int position = (Integer) buttonView.getTag();
                    objects.get(position).setChecked(buttonView.isChecked());
                }
            });
            holder.cb.setChecked(isChecked(position));

            return view;
        }

始终记住,在设置数据之前使用更改 holder.cb.setOnCheckedChangeListener() ie 任何侦听器,在我们的例子中是 holder .cb.setChecked()

原因:当我们滚动时,listview将回收视图,因此如果在侦听器之前使用setchecked,那么它将根据旧侦听器选择值。如果我们在侦听器之后设置它,那么它将采用最新值

编辑部分
下面的部分将展示如何使用 isChecked()setChecked() 方法来检索已检查的数据

/*
 * This function is in your Custom Adapter Class
 */
private boolean isChecked(int position){
    return object(position).isChecked();
}


/**
 * Getter Setter Class / Data Model Class that defines your object
 */
private class MyObject{
    private boolean isChecked;
    private String a, b, c, orWhateverYourObjectNeeds;

    public void setChecked(boolean isChecked){
        this.isChecked = isChecked;
    }

    public boolean isChecked(){
        return isChecked
    }
}

Check the code below -

public View getView(int position, View convertView, ViewGroup parent){
            View view = convertView;
            ViewHolder holder = new ViewHolder();

            if(view == null){

                view = inflater.inflate(R.layout.list_callcycle_blue, null);                
                holder.llContainer = (LinearLayout) view.findViewById(R.id.ll_container);
                holder.lblLabel = (TextView) view.findViewById(R.id.txt_desc);
                holder.cb = (CheckBox) view.findViewById(R.id.cb_store);

                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            final Object data = getItem(position);
            holder.lblLabel.setText(data.getDescription());

            holder.cb.setTag(position);
            holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    int position = (Integer) buttonView.getTag();
                    objects.get(position).setChecked(buttonView.isChecked());
                }
            });
            holder.cb.setChecked(isChecked(position));

            return view;
        }

Always keep in mind, use change holder.cb.setOnCheckedChangeListener() i.e. any listener before it's setting data, in our case it is holder.cb.setChecked()

Reason : When we scroll, listview will recycle the views, so if setchecked is used before listeners then it will pick values on the basis of old listener. And if we set it after listener, then it will take latest values

EDITED PART
Below part will show how isChecked() and setChecked() methods used for retrieving checked data

/*
 * This function is in your Custom Adapter Class
 */
private boolean isChecked(int position){
    return object(position).isChecked();
}


/**
 * Getter Setter Class / Data Model Class that defines your object
 */
private class MyObject{
    private boolean isChecked;
    private String a, b, c, orWhateverYourObjectNeeds;

    public void setChecked(boolean isChecked){
        this.isChecked = isChecked;
    }

    public boolean isChecked(){
        return isChecked
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文