How to get all checked items in a listView in Android?

This example demonstrates how do I get all checked items in a listView in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.



   
   
   
   
   

Step 3 − Create a java class (CustomAdapter.java) and the following code −

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Objects;
class CustomAdapter extends BaseAdapter {
   private Context context;
   private static ArrayList modelArrayList;
   CustomAdapter(Context context, ArrayList modelArrayList) {
      this.context = context;
      CustomAdapter.modelArrayList = modelArrayList;
   }
   @Override
   public int getViewTypeCount() {
      return getCount();
   }
   @Override
   public int getItemViewType(int position) {
      return position;
   }
   @Override
   public int getCount() {
      return modelArrayList.size();
   }
   @Override
   public Object getItem(int position) {
      return modelArrayList.get(position);
   }
   @Override
      public long getItemId(int position) {
      return 0;
   }
   @SuppressLint("InflateParams")
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      final ViewHolder holder;
      if (convertView == null) {
         holder = new ViewHolder();
         LayoutInflater inflater = (LayoutInflater)
         context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = Objects.requireNonNull(inflater).inflate(R.layout.listitem, null, true);
         holder.checkBox = convertView.findViewById(R.id.checkBox);
         holder.tvPlayer = convertView.findViewById(R.id.playerNameList);
         convertView.setTag(holder);
      } else {
         holder = (ViewHolder) convertView.getTag();
      }
      holder.checkBox.setText("Checkbox " + position);
      holder.tvPlayer.setText(modelArrayList.get(position).getPlayer());
      holder.checkBox.setChecked(modelArrayList.get(position).getSelected());
      holder.checkBox.setTag(R.integer.btnPlusView, convertView);
      holder.checkBox.setTag(position);
      holder.checkBox.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Integer pos = (Integer) holder.checkBox.getTag();
            Toast.makeText(context, "Checkbox " +pos+ "Clicked!",
            Toast.LENGTH_SHORT).show();
            if (modelArrayList.get(pos).getSelected()) {
               modelArrayList.get(pos).setSelected(false);
            } else {
               modelArrayList.get(pos).setSelected(true);
            }
         }
      });
      return convertView;
   }
   private class ViewHolder {
      CheckBox checkBox;
      private TextView tvPlayer;
   }
}

Step 4 − Create a java class (Model.java) and the following code −

class Model {
   private boolean isSelected;
   private String player;
   String getPlayer() {
      return player;
   }
   void setPlayer(String player) {
      this.player = player;
   }
   boolean getSelected() {
      return isSelected;
   }
   void setSelected(boolean selected) {
      isSelected = selected;
   }
}

Step 5 − Add the following code in res/values/strings.xml


   Sample
   1
   2

Step 6 − Create a layout for you listView (listItem.xml) and add the following code −



   
      


Step 7 − Add the following code to src/MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
   private ListView listView;
   private ArrayList modelArrayList;
   private CustomAdapter customAdapter;
   Button btnSelect, btnDeselect;
   public static String[] playerList = new String[]{"Sunil Chetri - INDIA",
      "Cristiano Ronaldo - Portugal",
      "Lionel Messi - Argentina",
      "Neymar Jr - Brazil",
      "Eden Hazard - Belgium",
      "Gigi Buffon - Italy",
      "James Rodrigues - Columbia",
      "Sadio Mane - Senegal",
      "Toni Kroos - Germany"};
      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         listView = findViewById(R.id.listView);
         btnSelect = findViewById(R.id.viewCheckedItem);
         btnDeselect = findViewById(R.id.deSelect);
         modelArrayList = getModel(false);
         customAdapter = new CustomAdapter(MainActivity.this, modelArrayList);
         listView.setAdapter(customAdapter);
         btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               modelArrayList = getModel(true);
               customAdapter = new CustomAdapter(MainActivity.this, modelArrayList);
               listView.setAdapter(customAdapter);
               Toast.makeText(getApplicationContext(), "Checked all items",
               Toast.LENGTH_SHORT).show();
            }
         });
         btnDeselect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               modelArrayList = getModel(false);
               customAdapter = new CustomAdapter(MainActivity.this,modelArrayList);
               listView.setAdapter(customAdapter);
               Toast.makeText(getApplicationContext(), "Unchecked all items",
               Toast.LENGTH_SHORT).show();
            }
         });
      }
      private ArrayList getModel(boolean isSelect) {
         ArrayListlist = new ArrayList();
         for (int i = 0; i 

Step 8 − Add the following code to androidManifest.xml



   
      
         
            
            
         
      
   

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click the RunPlay Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Updated on: 2020-07-07T13:45:41+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements