title: ListItems doubles in number(duplicated) in Custom ListView in android
body: <p>I am using a custom <code>ListView</code> which gets doubled in number(duplicate listItems) when I go to previous activity and again come to that ListViewActivity. </p>
  
  <p>I can see where the problem occurs. When I go to the ListViewActivity for the first time everything is fine where the listitems are added to the <code>ArrayList</code> and the adapter is set. But when I go to the previous activity and come again to the ListViewActivity, the previously set listitems are still present in the <code>Adapter</code> and again the listitems are set when the process goes through <code>setAdapter()</code> which is causing the duplication of listitems.</p>
  
  <p>I have came across many solutions like using <code>notifyDataSetChanged()</code>, clearing the <code>ArrayList</code>, <code>setAdapter(null)</code>, etc. None of these solutions helped me. </p>
  
  <p>I guess the solution is clearing the <code>Adapter</code>and the <code>ArrayList</code> and refreshing the <code>Adapter</code>. But I don't know how and where exactly to do it.</p>
  
  <p>Here is my code:</p>
  
  <p>This is the data recieving class in the LoadingActivity.java </p>
  
  <pre><code>private class CategoriesAsyncTask extends AsyncTask&lt;String,Integer,Double&gt;{
  
       String res="";
       String curState ="";
       String httppoststr="";
       boolean isNxtActivity=false;
  
       @Override
      protected Double doInBackground(String... params) {
          // TODO Auto-generated method stub
  
          postData(params[0]);
          return null;
  
       }
       protected void onPostExecute(Double Result)
       {  
          if(isNxtActivity)
          {   
              Intent intent = new Intent(LoadingActivity.this, ListItemsActivity.class);
              if(curState.equals(Constants.STATE_GET_STOCK_ITEMS))
              {
                  intent.putStringArrayListExtra(Constants.STOCK_ITEMS_LIST, listOfItems);
                  intent.putExtra("curState", Constants.STATE_GET_STOCK_ITEMS);
                  intent.putExtra(Constants.CATEGORY_ID, categoryId);
                  intent.putExtra(Constants.CUSTOMER_ID, customerId);
                  LoadingActivity.this.startActivity(intent);
              }
          }
      }
  
      public void postData(String ValueIWantToSend) {
          // Create a new HttpClient and Post Header
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(Constants.GatewayUrl);
          try
          {
              JSONObject json = new JSONObject(ValueIWantToSend);
              curState = json.getString("curState");
              StringEntity se = new StringEntity(ValueIWantToSend);
              httppost.setEntity(se);
              httppost.setHeader("Accept", "application/json");
              httppost.setHeader("Content-type", "application/json");
              httppoststr =httppost.toString();
              // Execute HTTP Post Request
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
              res = EntityUtils.toString(entity, "UTF-8");
              JSONObject jsonobj = new JSONObject(res);
              String state = jsonobj.getString(Constants.STATE);
  
              if(Constants.RESPONSE_SUCCESS.equals(state.toLowerCase()))
              {
                  isNxtActivity =true;
                  if(curState.equals(Constants.STATE_GET_STOCK_ITEMS))
                  {
                      JSONArray itemsListJsonArray=new JSONArray(); 
                      itemsListJsonArray=jsonobj.getJSONArray(Constants.STOCK_ITEMS_LIST);
  
                      for(int i=0;i&lt;itemsListJsonArray.length();i++)
                      {
                          String itemslist=itemsListJsonArray.getString(i);
                          listOfItems.add(itemslist.toString());
                      }
                  }
              }
          }
           catch (ClientProtocolException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              catch (JSONException e) {
                  e.printStackTrace();
              }
      }
  }
  </code></pre>
  
  <p>ListViewActivity.java</p>
  
  <pre><code>private String[] arrayOfNames;
  private String[] arrayOfImageUrls;
  
  ArrayList&lt;String&gt; listOfNames = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; listOfImageUrls = new ArrayList&lt;String&gt;();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_list_items);
  
      itemsList=(ListView) findViewById(R.id.itemsList);
  
      Bundle extras = getIntent().getExtras();
      listOfItems = extras.getStringArrayList(Constants.STOCK_ITEMS_LIST); //The duplication occurs here while getting the data from the LoadingActivity when visiting this activity for the second time.
  
      arrayOfNames = new String[listOfNames.size()];
      for(int i=0;i&lt;listOfNames.size();i++)
      {
          arrayOfNames[i]=listOfNames.get(i);
      }
  
      arrayOfImageUrls = new String[listOfImageUrls.size()];
      for(int i=0;i&lt;listOfImageUrls.size();i++)
      {
          arrayOfImageUrls[i]=listOfImageUrls.get(i);
      } 
  
      listItemadapter = new CustomListItemsAdapter(this, arrayOfNames, arrayOfImageUrls); 
      itemsList.setAdapter(listItemadapter);
  }
  </code></pre>
  
  <p>CustomAdapter.java</p>
  
  <pre><code>public class CustomListItemsAdapter extends BaseAdapter{
  
  private Context context;
  private LayoutInflater inflater;
  private String[] arrayOfNames;
  
  public CustomListItemsAdapter(ListItemsActivity listItemsActivity, String[] itemsName) {
      // TODO Auto-generated constructor stub
  
      context=listItemsActivity;
      arrayOfNames = itemsName;
  
      inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
  
  @Override
  public int getCount() {
      // TODO Auto-generated method stub
      return arrayOfNames.length;
  }
  
  @Override
  public Object getItem(int position) {
      // TODO Auto-generated method stub
      return position;
  }
  
  @Override
  public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
  }
  
  public static class ViewHolder
  {
     private TextView itemName;
     private ImageView itemImage;
  }
  
   @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
  
      ViewHolder holder;     
  
      if(convertView==null)
      {
          holder=new ViewHolder();
          convertView = inflater.inflate(R.layout.list_item_content, parent, false);
  
          holder.itemName = (TextView) convertView.findViewById(R.id.itemName);
          holder.itemImage = (ImageView) convertView.findViewById(R.id.itemImage);       
  
          convertView.setTag(holder);
      }
      else
      {
          holder = (ViewHolder) convertView.getTag();
      }
  
      holder.itemName.setText(arrayOfNames[position]);
      StrictMode.ThreadPolicy policy = new
              StrictMode.ThreadPolicy.Builder()
              .permitAll().build();
              StrictMode.setThreadPolicy(policy);
  
      ListItemsAsyncTask listAsync = new ListItemsAsyncTask();
      Drawable drawableIcon = listAsync.loadImageFromServer(arrayOfImageUrls[position]);
      holder.itemImage.setImageDrawable(drawableIcon);
  
   return convertView;
  }
  
  private class ListItemsAsyncTask extends AsyncTask&lt;String, Integer, Double&gt; {
  
      String httpPostStr, res;
      Boolean NextActivity = false;
  
      public Drawable loadImageFromServer(String url) 
      {
         try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable drawable = Drawable.createFromStream(is, "src name");
            return drawable;
         } catch (Exception e) {
            e.printStackTrace(); 
            return null;
         }
      }
  
      @Override
      protected Double doInBackground(String... params) {
          return null;
      }
  }
  </code></pre>
  
  <p>Any Suggestions???</p>
  
  <p><strong>UPDATE:</strong> I have found the problem behind this duplication. The second time when I come to the ListViewActivity the duplication occurs in the Bundle from which I get the data for the <code>ListView</code> and its contents.</p>
  
  <p>Now I need a solution to stop the duplication in the Bundle. I have mentioned the duplication occuring line with a comment in ListViewActivity.java</p>