title: Listview with Checkbox,RadioButton,Textview and button not working correctly in android
body: <p>I am creating a android app the UI of my application is below given.</p>
  
  <p><strong>On clicking of submit button, I need the selected check box, and radio buttons value.</strong></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>
  
  <ol>
  <li>When I scroll the list view radio button gets automatically selected or deselected not able to maintain the state of radio button.</li>
  <li>On click of button not getting the selected radio button as well as check box.</li>
  </ol>
  
  <p>Below is my layout as well as java program. Suggest me to get the correct values.</p>
  
  <p><img src="https://i.stack.imgur.com/rEGEF.png" alt="application image"></p>
  
  <p><strong>Main.xml</strong></p>
  
  <p>
  </p>
  
  <pre><code>&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>
  
  <p></p>
  
  <p><strong>row.xml</strong></p>
  
  <p>
  </p>
  
  <pre><code>&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>
  
  <p></p>
  
  <p><strong>MyAdaptor.java</strong></p>
  
  <pre><code>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;
      }
  
      static class ViewHolder {
          protected TextView text;
          protected CheckBox checkbox;
  
      }
  
      @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
                      .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
                          @Override
                          public void onCheckedChanged(CompoundButton buttonView,
                                  boolean isChecked) {
                              int getPosition = (Integer) buttonView.getTag();
                              list.get(getPosition).setSelected(
                                      buttonView.isChecked());
                          }
                      });
              convertView.setTag(viewHolder);
              convertView.setTag(R.id.label, viewHolder.text);
              convertView.setTag(R.id.check, viewHolder.checkbox);
  
          } else {
              viewHolder = (ViewHolder) convertView.getTag();
          }
          viewHolder.checkbox.setTag(position);
  
          viewHolder.text.setText(list.get(position).getName());
          viewHolder.checkbox.setChecked(list.get(position).isSelected());
  
          return convertView;
      }
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity implements OnItemClickListener {
  
      ListView listView;
      ArrayAdapter&lt;Model&gt; adapter;
      List&lt;Model&gt; list = new ArrayList&lt;Model&gt;();
  
      private RadioGroup radioCcToGroup;
      private RadioButton radioTypeButton;
      private Button btn;
  
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          setContentView(R.layout.main);
  
          listView = (ListView) findViewById(R.id.my_list);
  
          btn = (Button) findViewById(R.id.submit);
  
          btn.setOnClickListener(new View.OnClickListener() {
              int count = 0;
  
              @Override
              public void onClick(View view) {
  
  
                  count = listView.getAdapter().getCount();
                  for (int i = 0; i &lt; count; i++) {
                      // here i am not able to get the records as getting on onItemClick of the listview
                  }
              }
          });
  
          adapter = new MyAdapter(this, getModel());
          listView.setAdapter(adapter);
          listView.setOnItemClickListener(this);
      }
  
      @Override
      public void onItemClick(AdapterView&lt;?&gt; arg0, View v, int position, long arg3) {
          TextView label = (TextView) v.getTag(R.id.label);
          CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
          Toast.makeText(v.getContext(),
                  label.getText().toString() + " " + isCheckedOrNot(checkbox),
                  Toast.LENGTH_LONG).show();
  
      }
  
      private String isCheckedOrNot(CheckBox checkbox) {
          if (checkbox.isChecked())
              return "is checked";
          else
              return "is not checked";
      }
  
      private List&lt;Model&gt; getModel() {
          list.add(new Model("Linux"));
          list.add(new Model("Windows7"));
          list.add(new Model("Suse"));
          list.add(new Model("Eclipse"));
          list.add(new Model("Ubuntu"));
          list.add(new Model("Solaris"));
          list.add(new Model("Android"));
          list.add(new Model("iPhone"));
          list.add(new Model("Java"));
          list.add(new Model(".Net"));
          list.add(new Model("PHP"));
          return list;
      }
  </code></pre>
  
  <p><strong>Model.java</strong></p>
  
  <pre><code>private String name;
      private boolean selected;
  
      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;
      }
  </code></pre>