0
votes

How to select multiple list items in horizontal listview. I want to select multiple items in a list and store these values in an array.

        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) {

Code to change selected item color in class 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) {


            }

        }

    }

This is my Size Adapter code to change background and text color of selected item.

        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;

    }
2

2 Answers

1
votes

Make a bean class like this for your list

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;
}
}

Then make a arrayList like this

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

Then make a custom adapter to make it the data list for your list

then if checkbox is checked set the checked boolean to true for that position like this

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

when you want to make another arraylist run a for loop for the listArray and check if the boolean is true or not, if its true then add it to another array list that you want to make.

0
votes

First of all you need to make arraylist of the listView with help of a beanClass where one is item data and other is boolean which shows if the field is selected or not.

On item selected you need to change the value of the boolean to true, which will help you to handle the selected items.

And whenever you need to add the selected item data to a new arraylist you can run a for loop for the list array and can check the value of the boolean.