【问题标题】:I want to change list item color when selected or deselected and store all selected values in array我想在选择或取消选择时更改列表项颜色并将所有选定值存储在数组中
【发布时间】:2017-05-17 17:31:31
【问题描述】:

如何在水平列表视图中选择多个列表项。我想在列表中选择多个项目并将这些值存储在一个数组中。

        Async task to show sizelist

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (dialog != null) {
                dialog.dismiss();
                dialog = null;
            }

            if (flag == 1) {

                sizeAdapter = new SizeAdapter(Product.this, arraysize, "fonts/GeosansLight.ttf");
                list_size.setAdapter(sizeAdapter);

                list_size.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

更改类中所选项目颜色的代码 HomeGS homeGS = arraysize.get(position);

                        if (homeGS.getSelected()) {

                            homeGS.setSizeProduct(homeGS.getSizeProduct());
                            homeGS.setSelected(false);


                        } else {
                            homeGS.setSizeProduct(homeGS.getSizeProduct());
                            homeGS.setSelected(true);
                        }

                        arraysize.set(position, homeGS);
                        sizeAdapter.notifyDataSetChanged();
                        size = arraysize.get(position).getSizeProduct();

                    }

                });


            } else if (flag == 0) {


            }

        }

    }

这是我的尺寸适配器代码,用于更改所选项目的背景和文本颜色。

        if (oldData.get(position).getSelected()){

            holder.txt_name.setText(oldData.get(position).getSizeProduct());
            holder.txt_name.setBackgroundColor(Color.parseColor("#000000"));
            holder.txt_name.setTextColor(Color.parseColor("#ffffff"));

        }else {

            holder.txt_name.setText(oldData.get(position).getSizeProduct());
            holder.txt_name.setBackgroundResource(R.drawable.btn_border);
            holder.txt_name.setTextColor(Color.parseColor("#000000"));

        }
        return convertView;

    }

【问题讨论】:

    标签: android listview arraylist checkbox


    【解决方案1】:

    为你的列表创建一个这样的 bean 类

    public class YourBean {
    
    private String itemData;
    private boolean checked;
    
    
    public String getitemData() {
        return itemData;
    }
    
    public void setitemData(String itemData) {
        this.itemData= itemData;
    }
    
    public boolean isChecked() {
        return checked;
    }
    
    public void setChecked(boolean checked) {
        this.checked = checked;
    }
    }
    

    然后做一个这样的arrayList

    ArrayList<YourBean> listArray = new ArrayList<>();
    

    然后制作一个自定义适配器,使其成为您列表的数据列表

    然后如果选中复选框,则将选中的布尔值设置为该位置的 true,如下所示

    listArray.get(position).setChecked(true);
    

    当你想创建另一个数组列表时,为 listArray 运行一个 for 循环并检查布尔值是否为真,如果为真,则将其添加到你要创建的另一个数组列表中。

    【讨论】:

    【解决方案2】:

    首先,您需要在 beanClass 的帮助下创建 listView 的数组列表,其中一个是项目数据,另一个是布尔值,显示该字段是否被选中。

    在选中的项目上,您需要将布尔值更改为 true,这将帮助您处理选中的项目。

    每当您需要将所选项目数据添加到新的数组列表时,您都可以为列表数组运行 for 循环并检查布尔值。

    【讨论】:

      猜你喜欢
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2012-05-16
      相关资源
      最近更新 更多