title: Checkbox unchecked when I scroll listview on Android
body: <p>I am new to Android development. I created a <code>listview</code> with a <code>textbox</code> and <code>checkbox</code>.</p>
  <p>When I check the <code>checkbox</code> and scroll it down to check some other items in the list view, the older ones are unchecked.</p>
  <p>How do I avoid this problem in a <code>listview</code>? Please guide me with my code.</p>
  <p>Here is the code:</p>
  <h3>main.xml:</h3>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:orientation=&quot;vertical&quot;
      android:layout_width=&quot;fill_parent&quot;
      android:layout_height=&quot;fill_parent&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/TextView01&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;List of items&quot;
          android:textStyle=&quot;normal|bold&quot;
          android:gravity=&quot;center_vertical|center_horizontal&quot;
          android:layout_width=&quot;fill_parent&quot;/&gt;
  
      &lt;ListView
          android:id=&quot;@+id/ListView01&quot;
          android:layout_height=&quot;250px&quot;
          android:layout_width=&quot;fill_parent&quot;/&gt;
  
      &lt;Button
          android:text=&quot;Save&quot;
          android:id=&quot;@+id/btnSave&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <hr />
  <p>This is the XML page I used to create dynamic list row:</p>
  <h3>listview.xml:</h3>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:gravity=&quot;left|center&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:paddingBottom=&quot;5px&quot;
      android:paddingTop=&quot;5px&quot;
      android:paddingLeft=&quot;5px&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/TextView01&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:gravity=&quot;center&quot;
          android:textColor=&quot;#FFFF00&quot;
          android:text=&quot;hi&quot;/&gt;
  
      &lt;TextView
          android:text=&quot;hello&quot;
          android:id=&quot;@+id/TextView02&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginLeft=&quot;10px&quot;
          android:textColor=&quot;#0099CC&quot;/&gt;
  
      &lt;EditText
          android:id=&quot;@+id/txtbox&quot;
          android:layout_width=&quot;120px&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:textSize=&quot;12sp&quot;
          android:layout_x=&quot;211px&quot;
          android:layout_y=&quot;13px&quot;/&gt;
  
      &lt;CheckBox
          android:id=&quot;@+id/chkbox1&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <hr />
  <p>This is my activity class.</p>
  <h3>CustomListViewActivity.java:</h3>
  <pre><code>public class CustomListViewActivity extends Activity {
  
      ListView lstView;
      static Context mContext;
      Button btnSave;
  
      private static class EfficientAdapter extends BaseAdapter {
          private LayoutInflater mInflater;
  
          public EfficientAdapter(Context context) {
              mInflater = LayoutInflater.from(context);
  
          }
  
          public int getCount() {
              return country.length;
          }
  
          public Object getItem(int position) {
              return position;
          }
  
          public long getItemId(int position) {
              return position;
          }
  
          public View getView(int position, View convertView, ViewGroup parent) {
              final ViewHolder holder;
              if (convertView == null) {
                  convertView = mInflater.inflate(R.layout.listview, parent,
                          false);
                  holder = new ViewHolder();
                  holder.text = (TextView) convertView
                          .findViewById(R.id.TextView01);
                  holder.text2 = (TextView) convertView
                          .findViewById(R.id.TextView02);
                  holder.txt = (EditText) convertView.findViewById(R.id.txtbox);
                  holder.cbox = (CheckBox) convertView.findViewById(R.id.chkbox1);
  
                  convertView.setTag(holder);
              }
              else {
                  holder = (ViewHolder) convertView.getTag();
              }
  
              holder.text.setText(curr[position]);
              holder.text2.setText(country[position]);
              holder.txt.setText(&quot;&quot;);
              holder.cbox.setChecked(false);
  
              return convertView;
          }
  
          public class ViewHolder {
              TextView text;
              TextView text2;
              EditText txt;
              CheckBox cbox;
          }
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          lstView = (ListView) findViewById(R.id.ListView01);
          lstView.setAdapter(new EfficientAdapter(this));
          btnSave = (Button)findViewById(R.id.btnSave);
          mContext = this;
  
          btnSave.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // I want to print the text which is in the listview one by one.
                  // Later I will insert it in the database.
                  //
                  //            Toast.makeText(getBaseContext(), &quot;EditText Value, checkbox value and other values&quot;, Toast.LENGTH_SHORT).show();
                  for (int i = 0; i &lt; lstView.getCount(); i++) {
                      View listOrderView;
                      listOrderView = lstView.getChildAt(i);
                      try{
                          EditText txtAmt = (EditText)listOrderView.findViewById(R.id.txtbox);
                          CheckBox cbValue = (CheckBox)listOrderView.findViewById(R.id.chkbox1);
                          if(cbValue.isChecked()== true){
                              String amt = txtAmt.getText().toString();
                              Toast.makeText(getBaseContext(), &quot;Amount is :&quot;+amt, Toast.LENGTH_SHORT).show();
                          }
  
                      }
                      catch (Exception e) {
                          // TODO: handle exception
                      }
                  }
  
              }
          });
      }
  
      private static final String[] country = { &quot;item1&quot;, &quot;item2&quot;, &quot;item3&quot;,
          &quot;item4&quot;, &quot;item5&quot;, &quot;item6&quot;,&quot;item7&quot;, &quot;item8&quot;, &quot;item9&quot;,
          &quot;item10&quot;, &quot;item11&quot;, &quot;item12&quot; };
  
      private static final String[] curr = { &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;,&quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;10&quot;, &quot;11&quot;, &quot;12&quot; };
  }
  </code></pre>
  <p>Please help me to solve this problem.</p>
  <p>I have referred to this problem in many places. But I could not get proper answer to solve this problem.</p>
  <p>Please provide me the code to avoid unchecking the checkbox while scrolling up and down.</p>