title: Android Listview items repeating
body: <p>My <code>listview</code> items keep on repeating every time the page loads. I think there is an error in my <code>adapter</code> class. Please help. Thank you in advance.</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.rows, 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.rows, 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();  // Here we get the position that we have set for the checkbox using setTag.
                      list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                  }
              });
              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); // This line is important.
  
          viewHolder.text.setText(list.get(position).getName());
          viewHolder.checkbox.setChecked(list.get(position).isSelected());
  
          return convertView;
      }
  }
  </code></pre>
  
  <p>I am filling the list with this function which i am calling in my fragment's onCreate method</p>
  
  <p>private List getModel() {</p>
  
  <pre><code>    list.clear();
      for(int i=0;i&lt;list1.size();i++){
          String a=list1.get(i);
          list.add(new Model(a));
      }
      return list;
  }
  </code></pre>
  
  <p>This is my fragment class</p>
  
  <pre><code>public class VaccAdmin extends FragmentActivity implements   OnItemClickListener{
  private Database dbHelper;    
  private double x,y;
  ListView listView;
  ArrayAdapter&lt;Model&gt; adapter;
  List&lt;Model&gt; list = new ArrayList&lt;Model&gt;();
  List&lt;String&gt; list1=new ArrayList&lt;String&gt;();
  
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.list);
      dbHelper=new Database(this);
      dbHelper.addVaccine();
      listView = (ListView) findViewById(R.id.my_list);
  
      String age = getIntent().getStringExtra("age");
      System.out.println(age+"Ageinmo");
      double ageinmo=Double.parseDouble(age);
      if(ageinmo&lt;=1.5){
          x=0.0;
          y=1.5;
      } else if(ageinmo&lt;=2.25){
          x=1.75;
          y=2.25;
      } else if(ageinmo&lt;=4.25){
          x=3.5;
          y=4.25;
      }else if(ageinmo&lt;=6.0){
          x=4.5;
          y=6.0;
      }else if(ageinmo&lt;=9.0){
          x=6.25;
          y=9.0;
      }else if(ageinmo&lt;=12.0){
          x=9.25;
          y=12.0;
      }else if(ageinmo&lt;=15.0){
          x=12.25;
          y=15.0;
      }else if(ageinmo&lt;=18.0){
          x=15.25;
          y=18.0;
      }else if(ageinmo&lt;=23.0){
          x=18.25;
          y=23.0;
      }else if(ageinmo&lt;=72.0){
          x=36.25;
          y=72.0;
      }else if(ageinmo&lt;=120.0){
          x=72.25;
          y=120.0;
      }else if(ageinmo&lt;=144.0){
          x=120.25;
          y=144.0;
      }else if(ageinmo&lt;=216.0){
          x=144.25;
          y=216.0;
      }
      list1=dbHelper.getVacNames(x, y);
      int size=list1.size();
      System.out.println("Size"+size);
      adapter = new MyAdapter(this,getModel());
      listView.setAdapter(null);
      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.clear();
      for(int i=0;i&lt;list1.size();i++){
          String a=list1.get(i);
          list.add(new Model(a));
      }
      return list;
  }
  </code></pre>
  
  <p>}</p>