title: how to check multiple checkboxes in a custom listview
body: <p>I have a ListView for which I have created a custom_row in xml. My custom_row contains an ImageView, 2 Textviews and a Checkbox.
  Data source for the listView is right now 3 arrays i.e. MedName, Descrip and Image of Meds.</p>
  
  <p>Now, when the app runs so the ListView fills up with data from Array.</p>
  
  <p><strong>Question:</strong> I want to allow the user to select as many medicines (via checkbox) as possible and when the submit button is pressed so all the selected (checked) items will be stored in separate table and unchecked in a separate table. BUT right now I've not attached any database so I want to display these checked and unchecked items in Toast.</p>
  
  <p>This is my custom_row (customlayout) code:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:padding="15sp"&gt;
  
  &lt;ImageView
      android:id="@+id/iv1"
      android:layout_width="60dp"
      android:layout_height="60dp"
      android:layout_alignParentStart="true"
      android:layout_alignParentTop="true"
      android:layout_marginStart="20dp"
      app:srcCompat="@mipmap/ic_launcher" /&gt;
  
  &lt;TextView
      android:id="@+id/tvName"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_marginStart="90dp"
      android:textSize="10dp"
      android:text="TextView" /&gt;
  
  &lt;TextView
      android:id="@+id/tvDescription"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/iv1"
      android:layout_marginStart="90dp"
      android:textSize="10dp"
      android:text="TextView" /&gt;
  
  &lt;CheckBox
      android:id="@+id/cbTaken"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/tvDescription"
      android:layout_marginStart="32dp"
      android:layout_toEndOf="@+id/tvDescription"
      android:text="Taken" /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>And this is my MainActivity code:</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  ListView lv1;
  CheckBox cbTaken;
  
  int imgs[] = {R.drawable.flagyl, R.drawable.imodium, R.drawable.flagyl, R.drawable.imodium};
  String[] medicinename = {"Flagyl", "Imodium", "Flagyl", "Imodium"};
  String[] usage = {"Gas Trouble", "Diarrohea", "Gas Trouble", "Diarrohea"};
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      lv1 = findViewById(R.id.lvCustom);
      cbTaken = findViewById(R.id.cbTaken);
  
      CustomAdapter customAdapter = new CustomAdapter();
      lv1.setAdapter(customAdapter);
  
  }
  
  public void showToast(View view) {
  
      String status = "";
  
      if (cbTaken.isChecked())
      {
          status = "Taken";
      }
      Toast.makeText(this, status, Toast.LENGTH_SHORT).show();
  
  
  }
  
  
  class CustomAdapter extends BaseAdapter
  {
  
      @Override
      public int getCount() { // used for size of our Data
          return imgs.length;
      }
  
      @Override
      public Object getItem(int i) {
          return null;
      }
  
      @Override
      public long getItemId(int i) {
  
  
          return 0;
      }
  
  
      @Override
      public View getView(int i, View view, ViewGroup viewGroup) {
  
          view = getLayoutInflater().inflate(R.layout.customlayout, null); 
  
          ImageView imgView= view.findViewById(R.id.iv1); //Now our layout is in View so we search inside view
          TextView tvName = view.findViewById(R.id.tvName);
          TextView tvDesc = view.findViewById(R.id.tvDescription);
  
          // NOW WE BIND THE DATA WITH IMageView
  
          imgView.setImageResource(imgs[i]);
          tvName.setText("Med Name: " + medicinename[i]);
          tvDesc.setText("Usage: " + usage[i]);
  
          return view;
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is the screenshot of running App<a href="https://i.stack.imgur.com/HUCIT.png" rel="nofollow noreferrer">CustomListView App</a></p>
  
  <p>I don't know how to add code in add comments options therefore I'm adding comments after @ManxDev reply here:
  This is what I have done</p>
  
  <pre><code>It is crashing my App
  cbTaken.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  
                  if (cbTaken.isChecked())
                  {
                      int posi = lv1.getSelectedItemPosition();
                      Toast.makeText(MainActivity.this, posi, Toast.LENGTH_SHORT).show();
                  }
  
              }
          });
  </code></pre>