简体   繁体   English

在复选框上更新列表项内容单击

[英]Update list item contents on checkbox click

I have a custom listview with item layout having a checkbox at the left and 4 textviews much like astrid task manager. 我有一个自定义列表视图,项目布局具有左侧的复选框和4个textview,非常类似于astrid任务管理器。 在此处输入图片说明

What I intend to do is that on clicking the checkbox, the textviews have to update their values. 我打算做的是,单击复选框后,textview必须更新其值。 At first, my problem was that on checking a checkbox at position 1 would check the checkbox at the end or somewhere else while scrolling. 最初,我的问题是,选中位置1的复选框会在滚动时在末尾或其他位置选中该复选框。 While searching for a solution, I came to know about recycling of view in listview. 在寻找解决方案时,我了解了Listview中视图的回收。 I applied those concepts like viewholder and right now I am able to maintain the state of checkbox. 我应用了诸如viewholder之类的概念,现在我可以保持复选框的状态。 But, on checkbox click, I change the textviews and the change is not persisting, meaning any random tv also shows the same change. 但是,在复选框上单击时,我更改了textviews,并且更改没有持久化,这意味着任何随机电视也显示相同的更改。

I have applied a checkbox onclicklistener in my adapter. 我在适配器中应用了一个复选框onclicklistener。 Any ideas how to achieve it? 有什么想法要实现吗?

You will want to persist the state of the item in your item data rather than in the view holder. 您将需要在项目数据中而不是在视图持有器中保留项目的状态。 The view holder is just a convenience to the view, which at various times will display many different items, assuming you recycle the views. 视图持有人只是视图的一种便利,假设您回收视图,视图持有人在不同时间将显示许多不同的项目。 During the data binding, you will want to bind all state necessary for displaying your item to avoid having random data appearing in another list item's view. 在数据绑定期间,您将需要绑定显示项目所需的所有状态,以避免在另一个列表项目的视图中出现随机数据。

public class MyListAdapter extends BaseAdapter {

    // ....

    @Override
    View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = inflate(R.layout.my_list_item_layout, parent, false);

            // View holder just prevents having to look up these values 
            // every time the view is reused.
            MyViewHolder holder = new MyViewHolder();
            holder.checkbox = (CheckBox) view.findViewById(R.id.check_box);
            holder.textview = (TextView) view.findViewById(R.id.text_view);
            view.setTag(holder);
        }

        final MyViewHolder holder = (MyViewHolder) view.getTag();
        final MyData data = (MyData) getItem(position);
        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                data.savedCheckboxState = isChecked;
                // now tell the view to rebind the data
                notifyDataSetChanged();
            }
        });

        holder.checkbox.setChecked(data.savedCheckboxState);
        holder.textview.setText(String.valueOf(data.savedCheckboxState));
        return view;
    }

    private static class MyViewHolder {
        CheckBox checkbox;
        TextView textview;
    }
}

You can refer to my full code here, what you can do is,store check box state in a boolean array then you wont face this issue..... 您可以在这里参考我的完整代码,您可以做的是,将复选框状态存储在布尔数组中,那么您将不会遇到此问题.....

Android checkbox multiselected issue Android复选框多选问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM