title: When I scroll the list view radio button gets automatically selected or deselected not able to maintain the state of radio button
body: <p>I am creating a android app the layout of my application is below given.</p>
  
  <p>On clicking of submit button, I need the selected check box, and radio buttons value.</p>
  
  <p>Example Linux is not checked, cc(radio button) is checked.</p>
  
  <p>Records are populated dynamically in list view, but I am not able to make it work. A lot of problems are there.</p>
  
  <p>1) When I scroll the list view radio button gets automatically selected or deselected not able to maintain the state of radio button.
     2) On click of button not getting the selected radio button as well as check box.</p>
  
  <p>Below is my layout as well as java program. Suggest me to get the correct values.</p>
  
  <p><a href="https://i.stack.imgur.com/y56Bo.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>Main.xml
  &lt;ListView
      android:id="@+id/my_list"
      android:layout_width="fill_parent"
      android:layout_height="199dp" /&gt;
  
  &lt;Button
      android:id="@+id/submit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Submit"
      /&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>row.xml
  &lt;TextView
      android:id="@+id/label"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@+id/label"
      android:layout_toRightOf="@+id/check"
      android:textSize="20sp" &gt;
  &lt;/TextView&gt;
  
  &lt;CheckBox
      android:id="@+id/check"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_marginLeft="4dip"
      android:layout_marginRight="10dip"
      android:focusable="false"
  
      android:focusableInTouchMode="false" &gt;
  &lt;/CheckBox&gt;
  
   &lt;RadioGroup
       android:id="@+id/radioSex"
       android:layout_width="wrap_content"
       android:layout_height="fill_parent"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"
       android:orientation="horizontal" &gt;
  
       &lt;RadioButton
           android:id="@+id/to"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dip"
           android:checked="true"
           android:text="To" /&gt;
  
       &lt;RadioButton
           android:id="@+id/cc"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dip"
           android:text="CC" /&gt;
   &lt;/RadioGroup&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>MyAdapter.java
  
  private class ViewHolder {
      protected TextView text;
      protected CheckBox checkbox;
      protected RadioGroup radioGroup;
  }
  
  public class MyAdapter extends ArrayAdapter&lt;Model&gt; {
  
      private final List&lt;Model&gt; list;
      private final Activity context;
      boolean checkAll_flag = false;
      boolean checkItem_flag = false;
  
      public MyAdapter(Activity context, List&lt;Model&gt; list) {
          super(context, R.layout.row, list);
          this.context = context;
          this.list = list;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          ViewHolder viewHolder = null;
          if (convertView == null) {
              LayoutInflater inflator = context.getLayoutInflater();
              convertView = inflator.inflate(R.layout.row, null);
  
              viewHolder = new ViewHolder();
              viewHolder.text = (TextView) convertView.findViewById(R.id.label);
              viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
              viewHolder.checkbox.setTag(position);
              viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioSex);
              viewHolder.radioGroup.setTag(position);
  
              convertView.setTag(viewHolder);
          } else {
              viewHolder = (ViewHolder) convertView.getTag();
          }
  
          viewHolder.text.setText(list.get(position).getName());
          viewHolder.checkbox.setChecked(list.get(position).isSelected());
          viewHolder.checkbox.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  CheckBox checkbox = (CheckBox) v;
                  int getPosition = (Integer) checkbox.getTag();
                  list.get(getPosition).setSelected(checkbox.isChecked());
              }
          });
  
          viewHolder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  
              @Override
              public void onCheckedChanged(RadioGroup group, int checkedId) {
                  boolean isCcOrIsTo = (checkedId == R.id.cc);
                  int getPosition = (Integer) group.getTag();
                  list.get(getPosition).setCcOrIsTo(isCcOrIsTo);
              }
          });
  
          return convertView;
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>Model.java
  
  public class Model {
  
      private String name;
      private boolean selected;
      private boolean isCcOrIsTo;
  
      public Model(String name) {
          this.name = name;
      }
  
      public String getName() {
          return name;
      }
  
      public boolean isSelected() {
          return selected;
      }
  
      public void setSelected(boolean selected) {
          this.selected = selected;
      }
  
      public boolean isCcOrIsTo() {
          return isCcOrIsTo;
      }
  
      public void setCcOrIsTo(boolean isCcOrIsTo) {
          this.isCcOrIsTo = isCcOrIsTo;
      }
  
      @Override
      public String toString() {
          String selectedString = selected ? "selected" : "not selected";
          String value = isCcOrIsTo ? "CC" : "To";
          return name+" -&gt; "+selectedString+ " with value "+value;
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>MainActivity.java
  
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.activity_main);
  
      listView = (ListView) findViewById(R.id.my_list);
  
      btn = (Button) findViewById(R.id.submit);
  
      btn.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View view) {
              for (Model m : list) {
                  Log.i("Stack1", m.toString());
              }
          }
      });
  
      ArrayAdapter&lt;Model&gt; adapter = new MyAdapter(this, getModel());
      listView.setAdapter(adapter);
  }</code></pre>
  </div>
  </div>
  </p>