Sunday 17 September 2023

How to get only selected checkboxes on button click in a RecyclerView android java. Explain with example

 To get only the selected checkboxes in a RecyclerView when a button is clicked in Android using Java, you can follow these steps:

  1. Create a RecyclerView in your XML layout file.
  2. Create a custom adapter for your RecyclerView that includes a checkbox for each item.
  3. Track the selected state of checkboxes in your data model.
  4. Implement the logic to update the selected state of checkboxes when they are clicked.
  5. Implement the logic to retrieve the selected checkboxes when the button is clicked.

Here's an example:

  1. Create your data model:

------------------------------------------------------------------------------------------------------------------------

public class MyItem {

    private String itemName;

    private boolean isSelected;


    public MyItem(String itemName) {

        this.itemName = itemName;

        this.isSelected = false;

    }


    public String getItemName() {

        return itemName;

    }


    public boolean isSelected() {

        return isSelected;

    }


    public void setSelected(boolean selected) {

        isSelected = selected;

    }

}

------------------------------------------------------------------------------------------------------------------------

Create a custom RecyclerView adapter:

------------------------------------------------------------------------------------------------------------------------

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {


    private List<MyItem> itemList;


    public MyAdapter(List<MyItem> itemList) {

        this.itemList = itemList;

    }


    @NonNull

    @Override

    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);

        return new ViewHolder(view);

    }


    @Override

    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        MyItem item = itemList.get(position);

        holder.itemNameTextView.setText(item.getItemName());

        holder.checkBox.setChecked(item.isSelected());


        holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {

            item.setSelected(isChecked);

        });

    }


    @Override

    public int getItemCount() {

        return itemList.size();

    }


    public List<MyItem> getSelectedItems() {

        List<MyItem> selectedItems = new ArrayList<>();

        for (MyItem item : itemList) {

            if (item.isSelected()) {

                selectedItems.add(item);

            }

        }

        return selectedItems;

    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView itemNameTextView;

        public CheckBox checkBox;


        public ViewHolder(View itemView) {

            super(itemView);

            itemNameTextView = itemView.findViewById(R.id.itemNameTextView);

            checkBox = itemView.findViewById(R.id.checkBox);

        }

    }

}

------------------------------------------------------------------------------------------------------------------------

Create your RecyclerView item layout (item_layout.xml) with a TextView and a CheckBox:

------------------------------------------------------------------------------------------------------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="horizontal">


    <TextView

        android:id="@+id/itemNameTextView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_vertical"

        android:textSize="16sp" />


    <CheckBox

        android:id="@+id/checkBox"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_vertical" />

</LinearLayout>


------------------------------------------------------------------------------------------------------------------------

In your activity or fragment, set up the RecyclerView and button click listener:

------------------------------------------------------------------------------------------------------------------------

public class MyActivity extends AppCompatActivity {


    private RecyclerView recyclerView;

    private MyAdapter adapter;

    private List<MyItem> itemList = new ArrayList<>();


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my);


        recyclerView = findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        // Create sample items

        itemList.add(new MyItem("Item 1"));

        itemList.add(new MyItem("Item 2"));

        itemList.add(new MyItem("Item 3"));


        adapter = new MyAdapter(itemList);

        recyclerView.setAdapter(adapter);


        Button button = findViewById(R.id.button);

        button.setOnClickListener(v -> {

            // Get selected checkboxes

            List<MyItem> selectedItems = adapter.getSelectedItems();

            for (MyItem item : selectedItems) {

                Log.d("Selected Item", item.getItemName());

            }

        });

    }

}


------------------------------------------------------------------------------------------------------------------------

In this example, when the button is clicked, the getSelectedItems method of the adapter is called to retrieve the selected checkboxes, and their corresponding items are logged to the console. You can modify this logic to suit your specific requirements or perform other actions with the selected items.

No comments:

Post a Comment