title: How to get position of checked checkbox from ListView in android
body: <blockquote>
    <p>I created custom <code>ListView</code> with <code>CheckBox</code> next to title. I want to get checked
    <code>CheckBox</code> from <code>ListView</code></p>
    
    <p>I created following function for get message from <code>ListView</code> and send it to another <code>ListView</code> with <code>Intent</code> but I cant get the particular <code>CheckBox</code> <em>position</em> to move.</p>
  </blockquote>
  
  <pre><code>public class SpamActivity extends AppCompatActivity {
  
  List&lt;Message&gt; sms;
  ArrayList&lt;Message&gt; sms1;
  ArrayList&lt;String&gt; al;
  ListView lv;
  CheckBox checkbox;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.messagebox);
  
      lv= (ListView) findViewById(R.id.lvMsg);
      checkbox = (CheckBox) findViewById(R.id.checkBox);
       sms = new ArrayList&lt;&gt;();
       al=new ArrayList&lt;&gt;(0);
      populateMessageList();
  
    /*  SpamActivity spamactivity = new SpamActivity();
      al = spamactivity.al;
      //InboxFragment inboxfragment = new InboxFragment();
      //al = inboxfragment.list;
  
      for (int i=0;i&lt;sms.size();i++){
          System.out.println("AddressSPS" + sms.get(i));
      }                */
      Collections.reverse(sms);
  
  }
  
  public void populateMessageList() {
       sms1 = new ArrayList&lt;&gt;();
      fetchDatabaseMessage();
  
      lv.setAdapter(new datalist(getApplicationContext(), sms1));
  
          // to handle click event on listView item
          lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              public void onItemClick(AdapterView&lt;?&gt; arg0, View v, int position, long arg3) {
                  // when user clicks on ListView Item , onItemClick is called
                  // with position and View of the item which is clicked
                  // we can use the position parameter to get index of clicked item
                  TextView textViewSMSSender = (TextView) v.findViewById(R.id.lblNumber);
                  TextView textViewSMSBody = (TextView) v.findViewById(R.id.lblMsg);
                  TextView textViewSMSDate = (TextView) v.findViewById(R.id.smsdate);
                  String smsSender = textViewSMSSender.getText().toString();
                  String smsBody = textViewSMSBody.getText().toString();
                  String smsDate = textViewSMSDate.getText().toString();
  
                  Intent intentspam = new Intent(getApplicationContext(), DisplaySpamsms.class);
                  intentspam.putExtra("number",smsSender);
                  intentspam.putExtra("msg",smsBody);
                  startActivity(intentspam);
  
              }
          });
  }
  
  // This method featch the message stored in database
  public  void fetchDatabaseMessage(){
      DB_Message dbmessage = new DB_Message(this);
      sms = dbmessage.ViewMessageData();
      String addr = sms.get(0).getmAddress();
      if (addr.equals(sms.get(0).getmAddress())) {
          for (int i = 0; i &lt; sms.size(); i++) {
              al.add(sms.get(i).getmAddress());
              //al.add(sms.get(i).getmBody());
          }
      }else {
          System.out.println("SMS Not Displayed");
  
      }
  }
  
  
  class datalist extends BaseAdapter {
  
      Context context;
      ArrayList&lt;Message&gt; arrayListsms;
  
      public datalist(Context context, ArrayList&lt;Message&gt; arrayListsms) {
          // TODO Auto-generated constructor stub
          this.context = context;
          this.arrayListsms = arrayListsms;
      }
  
      @Override
      public int getCount() {
          // TODO Auto-generated method stub
          return sms.size();
      }
  
      @Override
      public Object getItem(int position) {
          // TODO Auto-generated method stub
          return getItem(position);
      }
  
      @Override
      public long getItemId(int position) {
          // TODO Auto-generated method stub
          return 0;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          // TODO Auto-generated method stub
          LayoutInflater inflator = getLayoutInflater();
          View row;
          row = inflator.inflate(R.layout.row, parent, false);
          //  ImageView img1 = (ImageView) row.findViewById(R.id.imageView1);
          final CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
          final TextView txt1 = (TextView) row.findViewById(R.id.lblMsg);
          final TextView txt2 = (TextView) row.findViewById(R.id.lblNumber);
          final TextView txt3 = (TextView) row.findViewById(R.id.smsdate);
          //Long timestamp = Long.parseLong(sms.get(position).getmDate());
          Calendar mcalendar = Calendar.getInstance();
          //mcalendar.setTimeInMillis(timestamp);
          DateFormat mformatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
  
  
          txt1.setText(sms.get(position).getmBody());
          txt2.setText(sms.get(position).getmAddress());
          //+"\n"+mformatter.format(mcalendar.getTime())
          txt3.setText(mformatter.format(mcalendar.getTime()));
  
          final String msgBody = txt1.getText().toString();
          final String msgAddr = txt2.getText().toString();
          final String msgDate = txt3.getText().toString();
  
          checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  Message msg = new Message();
                  if (isChecked) {
                      //  selectedStrings.add(tv.getText().toString());
  
                      Toast.makeText(context, "" + msgBody, Toast.LENGTH_LONG).show();
  
                      System.out.println("Body" + msgBody);
                      System.out.println("Address" + msgAddr);
                      System.out.println("Date" + msgDate);
  
                        al.add(msgBody + msgAddr + msgDate);
                      //                        al.add(msgAddr);
                     //                        al.add(msgDate);
  
                      sms.remove(position);
                      ContentValues values = new ContentValues();
                      values.put("address", sms.get(position).getmAddress());
                      values.put("body", sms.get(position).getmBody());
                      String date = msg.getmDate();
                      SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss");
                      getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
  
                      notifyDataSetChanged();
                  } else {
                      //   selectedStrings.remove(tv.getText().toString());
                  }
  
              }
          });
  
  
          return row;
      }
  }
  
  }
  </code></pre>
  
  <blockquote>
    <p>When I used above function for sending message but error is that when I click on any <code>CheckBox</code> to send that message then automatically <code>Different</code> Message is sent to another activity. so, I want to get particular <code>message</code> position to sent it.</p>
  </blockquote>