How to Clear all the list view items of a Static Custom list view in android? <p><div class="snippet" data-lang="js" data-hide="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>package com.donateblood.blooddonation; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import java.util.ArrayList; import java.util.List; import butterknife.ButterKnife; import butterknife.InjectView; /** * Created by YouCaf Iqbal on 4/6/2016. */ public class MainGUI extends AppCompatActivity { public static ArrayList&lt;DonorPerson&gt; Donors = new ArrayList&lt;DonorPerson&gt;(); @InjectView(R.id.findppl) Button _findButton; GPSTracker gps; String bloodgroup=null; private double latitude; private double longitude; DB db; String test=""; DBCursor cursor; DBCollection collection; Database dataobj = new Database(); ArrayList allPPLlat = new ArrayList(); ArrayList allPPLlong = new ArrayList(); ArrayList allPPLNumbers = new ArrayList(); ArrayList allPPLNames = new ArrayList(); ArrayList allPPLImages = new ArrayList(); ArrayList allPPLEmails = new ArrayList(); ArrayList SelectedPPLlat = new ArrayList(); ArrayList SelectedPPLlong = new ArrayList(); public Spinner mySpinner; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.maingui); ButterKnife.inject(this); getSupportActionBar().setDisplayHomeAsUpEnabled(true); Spinner spinner =(Spinner) findViewById(R.id.spinner); String[] list = getResources().getStringArray(R.array.blood_type); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,R.layout.spinner_layout,R.id.txt,list); spinner.setAdapter(adapter); _findButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getCurrentLatLong(); dbAsync thrd = new dbAsync(); thrd.execute(); } //distance=Distance(lablat, lablong, curlat, curlong); }); } public void getCurrentLatLong(){ gps = new GPSTracker(MainGUI.this); if (gps.canGetLocation()) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); } } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId()==android.R.id.home){ finish(); } return super.onOptionsItemSelected(item); } public class dbAsync extends AsyncTask&lt;Void,Void,Void&gt;{ private ProgressDialog pDialog; @Override protected Void doInBackground(Void... voids) { getOtherLatLong(); return null; } @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MainGUI.this); pDialog.setMessage("Searching people nearby..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); pDialog.dismiss(); Intent intent = new Intent(getApplicationContext(), PeopleList.class); startActivity(intent); // Toast.makeText(getBaseContext(), "Near by latitudes "+SelectedPPLlat, Toast.LENGTH_LONG).show(); // Toast.makeText(getBaseContext(), "Near by longitudes "+SelectedPPLlong, Toast.LENGTH_LONG).show(); } } public void getOtherLatLong() { db = dataobj.getconnection(); collection = db.getCollection("UserDetails"); //mySpinner=(Spinner) findViewById(R.id.spinner); //bloodgroup = mySpinner.getSelectedItem().toString(); // BasicDBObject whereQuery = new BasicDBObject(); //whereQuery.put("employeeId", 5); cursor = collection.find(); while (cursor.hasNext()) { DBObject doc = cursor.next(); // Lats longs used in the next for Loop for calculation distances allPPLlat.add(doc.get("lat")); allPPLlong.add(doc.get("long")); // All these other arraylists are used to store object of a donor person allPPLNumbers.add(doc.get("number").toString()); allPPLNames.add(doc.get("Name").toString()); allPPLImages.add(doc.get("image").toString()); allPPLEmails.add(doc.get("email").toString()); } for(int i =0;i&lt;allPPLlat.size();i++){ double Dist= Distance((double)allPPLlat.get(i),(double)allPPLlong.get(i),latitude,longitude); Dist=Dist/1000; if(Dist&lt;20){ Donors.add(new DonorPerson(""+allPPLNames.get(i)+"", ""+allPPLEmails.get(i)+"" ,""+allPPLNumbers.get(i)+"" ,""+allPPLImages.get(i)+"")); } } } public double Distance(double lat1, double lon1, double lat2, double lon2) { double R = 6371.0; // km double dLat = (lat2 - lat1) * Math.PI / 180.0; double dLon = (lon2 - lon1) * Math.PI / 180.0; lat1 = lat1 * Math.PI / 180.0; lat2 = lat2 * Math.PI / 180.0; double a = Math.sin(dLat / 2.0) * Math.sin(dLat / 2.0) + Math.sin(dLon / 2.0) * Math.sin(dLon / 2.0) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double d = R * c; return d * 1000; // return distance in m } }</code></pre> </div> </div> </p> <p>I have a static custom view called <code>Donors</code> declared in one of my classes called <code>MainGUI</code>. Whenever the list is displayed and I go back and reopen the list, the same list items appear twice. I know this happens because the list items are being added to the listview again when I display it without clearing all the data items shown before. What I want is a way to remove all the existing data items when I reopen the list, so that the items are not duplicated when i access it again.</p> <pre><code>public class PeopleList extends AppCompatActivity { @Override public void onBackPressed() { MainGUI.Donors.clear(); // Clear all the Donors after search // this.notifyDatasetChanged(); super.onBackPressed(); } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.peoplelistview); getSupportActionBar().setDisplayHomeAsUpEnabled(true); populateListView(); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId()==android.R.id.home){ // MainGUI.Donors.clear(); // Clear all the Donors after search finish(); } return super.onOptionsItemSelected(item); } private void populateListView() { ArrayAdapter&lt;DonorPerson&gt; adapter = new MyListAdapter(); ListView list = (ListView) findViewById(R.id.DonorsListView); list.setAdapter(adapter); } private class MyListAdapter extends ArrayAdapter&lt;DonorPerson&gt; { public MyListAdapter() { super(PeopleList.this, R.layout.singlelistitemview,MainGUI.Donors); } @Override public View getView(int position, View convertView, ViewGroup parent) { // Make sure we have a view to work with (may have been given null) View itemView = convertView; if (itemView == null) { itemView = getLayoutInflater().inflate(R.layout.singlelistitemview, parent, false); } // Find the Donor to work with. DonorPerson currentPerson = MainGUI.Donors.get(position); //Set the Image of the Current Donor ImageView DonorImage = (ImageView)itemView.findViewById(R.id.image); //currentPerson.getImage(); byte[] decodedString = Base64.decode(currentPerson.getImage(), Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); DonorImage.setImageBitmap(decodedByte); //imageView.setImageResource(currentPerson.getIconID()); /* Fill the view ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon); imageView.setImageResource(currentCar.getIconID()); */ return itemView; } } } </code></pre>