【问题标题】:How to implement a button that gets all checkbox's state and adds the value of checked item into arraylist?如何实现一个获取所有复选框状态并将选中项的值添加到arraylist的按钮?
【发布时间】:2012-04-28 17:23:51
【问题描述】:

我一直在寻找解决问题的方法,但找不到,所以在这里。

我有一个使用 textview 和复选框组合显示的数据数组列表。这是使用自定义适配器完成的,该适配器基本上逐行膨胀。到目前为止,一切都很好,直到我想检索检查的数据

我从其他人那里了解到,由于视图的回收,他们的选中框在滚动时变为未选中状态。 对我来说,复选框的状态仍然存在,但我不知道如何在不滚动的情况下取消它们。当我选中一个框时,它在 UI 上是“选中”,但实际上直到我将选中的项目滚动出视图并再次返回时,它才会被识别。

目前,如果 list.get(position).isSelected() == true,我使用全局数组列表填充 list.get(position).getName()。但是你会看到这个数组列表保持为空,除非我将项目滚动出视图并再次返回。当未选中这些框时,这同样适用,它们保留在数组列表中,直到我滚动。我想到的是一个提交按钮,当它被点击时,它会获取所有复选框的当前状态,然后构造数组列表。

知道如何创建这样的东西吗?

public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.qlist, 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();
                //
                if(isChecked){
                    selctionCount++;
                }
                else if (!isChecked){
                    selctionCount--;
                }
                if(selctionCount > 2)
                {
                     Toast.makeText(context, "error, you checked more than 2!", Toast.LENGTH_LONG).show();
                     element.setSelected(false);
                }
                else
                {
                    element.setSelected(buttonView.isChecked());
                }
                System.out.println(selctionCount);
            }
        });
        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));

    } 
    else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());

    //This is the problem here! the selected arraylist (global) is empty even when i check the boxes, 
    //it only populates when i scroll the checked boxes out of view and back in! helphelp!
    holder.checkbox.setChecked(list.get(position).isSelected());
    if (list.get(position).isSelected() == true){
        selected.add(list.get(position).getName());
    }
    else if (list.get(position).isSelected() == false){
        selected.remove(list.get(position).getName());
    }
    return view;
}

}

【问题讨论】:

  • 嗨,谢谢链接,但我的问题是当我单击按钮时获取所有复选框的状态,无论是选中还是未选中,而不是选中每个框的按钮。我错过了什么吗?
  • 我已将代码添加到问题中。

标签: java android checkbox


【解决方案1】:

我已经搞砸了类似的问题。您最初可以在构造函数中调用一个函数,该函数将复选框的状态初始化为 false,并将所有状态保存在数组列表中。每当单击复选框时,只需选中该框并将其状态保存在布尔数组列表中。 稍后您可以从 arraylist 中检索您的状态。 如果您不明白,请告诉我,我会尝试发送一些代码行来帮助您...

这里有一段代码可以帮助你: 我将创建一个布尔数组列表。

private ArrayList<Boolean> itemChecked      = null;

然后我将在我的构造函数中将所有复选框的状态设置为 false:

    for (int i=0; i < no_of_elements.size(); i++) {
        itemChecked.add(i, false);
    }

设置复选框被点击时的实际选中状态:

        holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                itemChecked.set(position, isChecked);

            }
        });

【讨论】:

  • 我不太清楚你的意思,你说“每当单击复选框时,只需选中该框并将其状态保存在布尔数组列表中”你能告诉我你是如何保存它的状态的吗?因为除非我让它回收,否则我这样做的方式是行不通的。
  • 我已经发布了一些代码行。试试吧。它应该可以解决您的问题。
  • thx 你的回答很有帮助,但是我确实通过添加到侦听器内的选定数组列表而不是与 selected.add(element.getName());然而另一个问题出现了link
  • 所以我最终没有使用自定义适配器,而是使用 android 的 simple_list_item_multiple_choice 和 setChoiceMode 作为 CHOICE_MODE_MULTIPLE
  • 我相信THIS 会是一个问题,如果我要显示的不仅仅是一个每行带有复选框的文本框,因为 simple_list_item_multiple_choice 不会削减它
猜你喜欢
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 2014-04-29
相关资源
最近更新 更多