I am getting duplicate entries into ListView using ArrayList to populate and HashSet to remove duplicate elements, but still getting duplicate values like this:
Delhi
Mumbai
Delhi
Mumbai
Here is my code which i am using to parse JSON data into List and to remove duplicate entries
MainActivity.java:
public class MainActivity extends Activity {
ArrayList<Destination> destinationArrayList;
MainAdapter adapter;
Destination destination;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
destinationArrayList = new ArrayList<Destination>();
new JSONAsyncTask().execute("my api link");
Set<Destination> hashset = new HashSet<>();
hashset.addAll(destinationArrayList);
destinationArrayList.clear();
destinationArrayList.addAll(hashset);
ListView listview = (ListView)findViewById(R.id.list);
adapter = new MainAdapter(getApplicationContext(), R.layout.row, destinationArrayList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), destinationArrayList.get(position).getTitle().toString(), Toast.LENGTH_LONG).show();
}
});
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("data");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
destination = new Destination();
destination.setCity(object.getString("city"));
destinationArrayList.add(destination);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
And when i use this, even not getting single record
into ListView:
destinationArrayList = new ArrayList<Destination>();
new JSONAsyncTask().execute("my api url");
System.out.println("size of Arraylist with duplicates: " + destinationArrayList.size());
System.out.println(destinationArrayList);
HashSet<Destination> listToSet = new HashSet<Destination>(destinationArrayList);
ArrayList<Destination> listWithoutDuplicates = new ArrayList<Destination>(listToSet);
System.out.println("size of ArrayList without duplicates: " + listToSet.size());
System.out.println(listWithoutDuplicates);
Destination.java
public class Destination {
private String city;
..............
public Destination() {
// TODO Auto-generated constructor stub
}
public Destination(String city) {
super();
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}