title: Android ListView Repeating Old Data After Refresh
body: <p>I have a Fragment page listing data in the listview from a json through a webservice. I have a pull down <code>&lt;SwipeRefreshLayout&gt;</code>. i use to swipe the fragment page to refresh the data which which will call the webservice again and load the data in listview.</p>
  
  <p>The code below does not empty the old data after refresh. the data still remains in the listview and add the refreshed data to the listview below the old data.</p>
  
  <p><strong>to refresh method</strong> i call the service again</p>
  
  <pre><code>ArrayList&lt;DataListItem&gt; listMockData;
  DataBaseAdapter HListAdapter;
  
  swipeLayout.setOnRefreshListener(
              new SwipeRefreshLayout.OnRefreshListener() {
                  @Override
                  public void onRefresh() {
  
                      HListAdapter.clear();
                      listMockData.clear();
                      HListAdapter.notifyDataSetChanged();
                      requestWebService(currentPage);
                      new Handler().postDelayed(new Runnable() {
                          @Override
                          public void run() {
                              swipeLayout.setRefreshing(false);
                          }
                      }, 1000);
                  }
              });
  </code></pre>
  
  <p><strong>BaseAdapter</strong></p>
  
  <pre><code>public DataBaseAdapter(Context context, ArrayList&lt;DataListItem&gt; listData) {
      this.listData = listData;
      layoutInflater = LayoutInflater.from(context);
  }
  
  public void clear() {
      listData.clear();
  }
  @Override
  public int getCount() {
      return listData.size();
  }
  
  @Override
  public Object getItem(int position) {
      return listData.get(position);
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  public View getView(final int position, View convertView, ViewGroup parent)  
  {
   newsItem = listData.get(position);
   if (convertView == null) {
          holder = new ViewHolder();
          convertView = layoutInflater.inflate(R.layout.home_post_list, null);
          holder.results = (LinearLayout) convertView.findViewById(R.id.results);
  convertView.setTag(holder);
  
      } else {
          holder = (ViewHolder) convertView.getTag();
      }
  holder.results.setText(newsItem.getResults());
   holder.position = position;
      return convertView;
  }
  
   public static class ViewHolder {
   TextView results;
   }
  </code></pre>
  
  <p><strong>Edit</strong>  calling webservice</p>
  
  <pre><code>public void requestPosts(int pageId) {
      JSONObject pagedata = new JSONObject();
      try {
          pagedata.put("pageId", pageId);            
      } catch (JSONException e) {
          e.printStackTrace();
      }
      Volley.newRequestQueue(getActivity()).
      add(new JsonObjectRequest(com.android.volley.Request.Method.POST,
                  url, pagedata
                  , new com.android.volley.Response.Listener&lt;JSONObject&gt;() {
              @Override
              public void onResponse(JSONObject jsonArray) {
                  if (jsonArray != null) {
                      try {
                          JSONObject allPost = new JSONObject(jsonArray.toString());
                          contacts = allPost.optJSONArray("GetPostListResult");
                          for (int i = 0; i &lt; contacts.length(); i++) {
                              JSONObject c = contacts.getJSONObject(i);
  
                              String Result = c.getString("Result");
  
                              results[x] = Result;
  
                              x++;
                          }
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  } else{
                      Log.e("ServiceHandler", "Couldn't get any data from the url");
                  }
                  listMockData = new ArrayList&lt;HomeListItem&gt;();
                  DataListItem newsData = new DataListItem();
                  newsData.setResult(results[i]);
                  listMockData.add(newsData);
  
                  dataToListView();
              }
          });
          }
          public void dataToListView(){
  
          listView = (ListView) root.findViewById(R.id.custom_list);
          HListAdapter = new DataBaseAdapter(getActivity(), listMockData);
          listView.setAdapter(HListAdapter);
          }
  </code></pre>