Reputation: 1924
MyListView.java activity contains two arraylist: catalogueArrayList and catalogTempArrayList respectively.
ArrayList<Catalogue> catalogueArrayList = new ArrayList<Catalogue>();
ArrayList<Catalog> catalogTempArrayList = new ArrayList<Catalog>();
In onCreate() activity, listview is populated using catalogueArrayList. On update, new data is being pulled to second arrayList in background then added to catalogueArrayList. I don't want to clear my adapter before updating my listview. One idea is to remove duplicate items and use a temp arraylist to display new data. My problem is i don't know how to compare two arraylist, find the duplicates, remove them and create a third arraylist. Will this solution be efficient ? Will this consume times ?
Here is my catalogue class.
public class Catalogue {
public String client_tel;
public String product_name;
public String product_price;
public String product_desc;
public String product_img;
public Catalogue(String _client_tel, String _product_name,String _product_price,String _product_desc,String _product_img){
this.client_tel=_client_tel;
this.product_name=_product_name;
this.product_price=_product_price;
this.product_desc=_product_desc;
this.product_img=_product_img;
}
Catalog class is just a duplicate of this class.
== Update ==
Here is my pull update task
list.setOnUpdateTask(new OnUpdateTask() {
public void updateBackground() {
// simulate long times operation.
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void updateUI() {
catalogueArrayList.removeAll(catalogTempArrayList);
efficientAdapter.notifyDataSetChanged();
}
public void onUpdateStart() {
new loadDataAsync().execute();
}
});
and my efficient adapter constructor
public EfficientAdapter(Activity a, ArrayList<Catalog> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader = new ImageLoader(activity.getApplicationContext());
//imageLoader=new ImageLoader(activity.getApplicationContext());
imageLoader = ImageLoader.getInstance();
}
Upvotes: 0
Views: 276
Reputation: 1056
There are several ways to do this.
First, you should define equals
function in your Catalogue
and Catalog
classes. Check if the object is Catalogue or Catalog class, then compare.
public boolean equals(Object o){
if (o == null){
return false;
}
if (o instanceof Catalog){
Catalog c = (Catalog) o;
return c.product_name.equals(this.product_name)
}
if (o instanceof Catalogue){
Catalogue cg = (Catalogue) o;
return cg.product_name.equals(this.product_name)
}
return false;
}
Then you could use the Iterator<Catalogue>
to check whether or not is in the catalogTempArrayList
and remove it from the catalogueArrayList
. Or just use the removeAll
function from ArrayList
.
catalogueArrayList.removeAll(catalogTempArrayList);
If you want to remove the Catalog
instance from the catalogTempArrayList
. Use this instead:
catalogTempArrayList.removeAll(catalogueArrayList);
These functions cost O(n*m), being n and m the number of elements of the arrays.
Upvotes: 1
Reputation: 13520
Try using
catalogueArrayList.removeAll(catalogTempArrayList);
This will remove all the Catalogue
items that the two ArrayList
have in common from catalogueArrayList
.
Note: There will be no change in catalogTempArrayList
. It will still have all its items.
You should override the equals
and hashcode
method in your Catalogue
class.
Upvotes: 1