从ArrayList <catalog> Android </catalog>类型的两个ArrayList中删除重复项

时间:2014-05-26 07:12:23

标签: android android-arrayadapter

MyListView.java活动分别包含两个arraylist:catalogueArrayList和catalogTempArrayList。

ArrayList<Catalogue> catalogueArrayList = new ArrayList<Catalogue>();
ArrayList<Catalog> catalogTempArrayList = new ArrayList<Catalog>();

在onCreate()活动中,使用catalogueArrayList填充listview。在更新时,新数据将被提取到后台的第二个arrayList,然后添加到catalogueArrayList。在更新我的listview之前,我不想清除我的适配器。一个想法是删除重复的项目并使用临时arraylist来显示新数据。我的问题是我不知道如何比较两个arraylist,找到重复,删除它们并创建第三个arraylist。这个解决方案会有效吗?这会消耗多少时间吗?

这是我的目录类。

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;

    }

目录类只是此类的副本。

==更新==

这是我的拉取更新任务

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();

            }

        });

和我的高效适配器构造函数

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();


    }

2 个答案:

答案 0 :(得分:1)

尝试使用

catalogueArrayList.removeAll(catalogTempArrayList);

这将从Catalogue中删除这两个ArrayList共有的所有catalogueArrayList项。

注意: catalogTempArrayList不会有任何变化。它仍将包含所有物品。

您应该覆盖equals课程中的hashcodeCatalogue方法。

答案 1 :(得分:1)

有几种方法可以做到这一点。

首先,您应该在equalsCatalogue类中定义Catalog函数。检查对象是Catalog还是Catalog类,然后比较。

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;
}

然后,您可以使用Iterator<Catalogue>检查是否在catalogTempArrayList中,并将其从catalogueArrayList中删除。或者只使用removeAll中的ArrayList功能。

catalogueArrayList.removeAll(catalogTempArrayList);

如果要从Catalog中删除catalogTempArrayList实例。请改用:

catalogTempArrayList.removeAll(catalogueArrayList);

这些函数的成本 O(n * m) n m 数组元素的数量。