如何在具有多个复选框的列表视图中保存选中项目的值?

发布于 2024-12-17 06:41:50 字数 1462 浏览 0 评论 0原文

我正在寻找实现带有多个复选框的列表视图。用户应该能够选择多个复选框并单击“提交”按钮以继续操作。

我正在使用自定义适配器来显示我自己的列表视图并覆盖 getView() 方法。

到目前为止一切顺利,我可以显示带有复选框的列表,并且可以选择多个复选框,但是当我尝试获取所选列表项的详细信息时(使用 list.get(position). getName()),它开始弹跳。我的意思是,当我开始滚动列表视图以选择更多项目时,上面的值将更改为不同的项目,而不保留原始项目。

我的代码在这里:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.checkboxlayout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);

            viewHolder.checkbox         .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
    Model element = (Model) viewHolder.checkbox.getTag();
    element.setSelected(buttonView.isChecked());
//Toast.makeText(getContext(), "You just checked: "+list.get(position).getName(), Toast.LENGTH_SHORT).show();
    System.out.println("The element name is: "+element.getName());
    System.out.println("Name of the checked item is--outside loop:"+list.get(position).getName());

}

});

有没有办法限制或保留我选择的项目的值,即使在不断滚动检查其他项目后也是如此。

I'm looking to implement a list view with multiple check boxes. The user should be able to select multiple check boxes and click on 'submit' button to proceed further.

I'm using a custom adapter to display my own list view and override getView() method.

So far so good, I'm able to display the list with check boxes and I can select multiple check boxes, but when I'm trying to get the selected list item details (using the list.get(position).getName()), it is started bouncing. I mean, when I start scrolling the list view to select further items, the value of the above is changing to different items and not keeping the original.

My code is here:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.checkboxlayout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);

            viewHolder.checkbox         .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
    Model element = (Model) viewHolder.checkbox.getTag();
    element.setSelected(buttonView.isChecked());
//Toast.makeText(getContext(), "You just checked: "+list.get(position).getName(), Toast.LENGTH_SHORT).show();
    System.out.println("The element name is: "+element.getName());
    System.out.println("Name of the checked item is--outside loop:"+list.get(position).getName());

}

});

Is there a way to constrain or hold the value of the items I selected even after keep scrolling to check for other items.

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

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

发布评论

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

评论(2

笑梦风尘 2024-12-24 06:41:50

问题是你正在回收系统提供的convertView。如果它存在(它不是 null),这并不意味着它包含您期望的确切旧视图,但可能是另一个视图。

因此,系统可能会回收“勾选”复选框视图,其中旧复选框未勾选,反之亦然。我遇到过这种情况,因此关注这个问题可能会帮助您解决所面临的问题。

编辑:要解决此问题,如果您有回收的视图,您只需重新初始化视图属性,或者(不鼓励)只需忽略 ConvertView 参数并在每次调用 getView 方法时创建一个新视图

The problem is that you are recycling the convertView provided by the system. If it is present (it is not null) this does not mean that it contains the exact old view you'd expect but maybe another one.

So it may happen that the system is recycling a "ticked" checkbox view where the old checkbox was unticked and viceversa. I had this situation so focusing on this issue might help you solve the problem you're facing.

EDIT: to solve this issue you simply have to reinitialize view attributes if you have a recycled view or, (discouraged) simply ignore the convertView parameter and create a new view every time the getView method is called

暮年 2024-12-24 06:41:50

您不必编写自定义适配器或自定义视图。 Android 已经实现了您需要的功能,使用它可能会完全绕过您的问题。

您所要做的就是将 ListView 的“选择模式”设置为 CHOICE_MODE_MULTIPLEsetChoiceMode(int)

您可以使用以下方法检索选中的项目getCheckedItemCount() getCheckedItemIds()。更多信息请参见:android:choiceMode< /a>.

You don't have to write a custom adapter or a custom view. Android has already implemented the functionality you need, and using it may just bypass your problem entirely.

All you have to do is set the 'choice mode' of your ListView to CHOICE_MODE_MULTIPLE: setChoiceMode(int)

You can retrieve the checked items with methods like getCheckedItemCount() getCheckedItemIds(). More information here: android:choiceMode.

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