title: How to add the checked item in listView to ArrayList?
body: <p>I have a ListView with 12 items each item contains a TextView and a checkbox. I managed to keep the CheckBox check status persistent so that when I scroll up and down I do not lose the check status set to each CheckBox.</p>
  
  <p>The problem I am facing now is, for each item in the ListView I want to add the TextView to an ArrayList if its correspondeing CheckBox is checked, and if the corresponding CheckBox is unchecked, I should delete the TextView from the ArrayList if it is contained in it.</p>
  
  <p>In the below code of the getView() method, the line "Log.d(TAG, "chosen topic size: " + chosenTopics.size());" always returns zero??!!</p>
  
  <p>please help me to solve this issue.</p>
  
  <p><strong>getView()</strong>:  </p>
  
  <pre><code>@Override
  public View getView(final int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      View view = null;
  
      if (convertView == null) {
          LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
          view = layoutinflater.inflate(R.layout.list_items_layout, null);
  
          final ViewHolder holder = new ViewHolder();
  
          holder.tv = (TextView) view.findViewById(R.id.tvlist_topic);
          holder.cb = (CheckBox) view.findViewById(R.id.cbList_hook);
          holder.iv = (ImageView) view.findViewById(R.id.ivList_delete);
  
          holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  // TODO Auto-generated method stub
                  ItemDesign element = (ItemDesign) holder.cb.getTag();
                  element.setChecked(buttonView.isChecked());
  
                  if(element.isChecked()) {
                      if (!chosenTopics.contains(designList.get(position).getTopic()))
                          chosenTopics.add(designList.get(position).getTopic());
                  }
  
                  if (!element.isChecked()) {
                      if (!chosenTopics.isEmpty()) {
                      if (chosenTopics.contains(designList.get(position).getTopic()))
                          chosenTopics.remove(position);
                      }
                  }
              }
          });
  
          Log.d(TAG, "chosen topic size: " + chosenTopics.size());
          view.setTag(holder);
          holder.cb.setTag(designList.get(position));
  
      } else {
          view = convertView;
          ((ViewHolder) view.getTag()).cb.setTag(designList.get(position));
      }
  
      final ViewHolder holder = (ViewHolder) view.getTag();
      holder.tv.setText(designList.get(position).getTopic());
      holder.cb.setChecked(designList.get(position).isChecked());
  
      holder.iv.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              if (holder.cb.isChecked())
                  holder.cb.setChecked(false);
  
              designList.remove(position);
              notifyDataSetChanged();
          }
      });
  
      return view;
  }
  </code></pre>
  
  <p><strong>LogCat errors after moving initialisation of the ArrayList into the constructor</strong>:</p>
  
  <pre><code>02-12 14:40:14.117: E/AndroidRuntime(22500): FATAL EXCEPTION: main
  02-12 14:40:14.117: E/AndroidRuntime(22500): Process:  
  com.example.mqtt_designlayout_02, PID: 22500
  02-12 14:40:14.117: E/AndroidRuntime(22500):   
  java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
  02-12 14:40:14.117: E/AndroidRuntime(22500):    at 
  java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
  02-12 14:40:14.117: E/AndroidRuntime(22500):    at 
  java.util.ArrayList.remove(ArrayList.java:403)
  02-12 14:40:14.117: E/AndroidRuntime(22500):    at 
  </code></pre>
  
  <p>com.example.mqtt_designlayout_02.listviewadapter.ListViewAdapter$1.onCheckedChanged(ListViewAdapter.java:95)</p>