0

场景:我有两个包含应用程序详细信息的数组列表。一个 ArrayList 包含所有应用程序的集合,第二个 ArrayList 具有选定应用程序的集合。而且我必须从所有应用程序列表中删除选定的应用程序。为此,我使用了 ArrayList.removeAll 函数。

问题: ArrayList.removeAll 工作得非常好,但一个问题是,当两个应用程序的选定应用程序数组列表中有一个应用程序具有相同名称(但其他属性不同)时,例如。AngryBirds... 它从列表中删除这两个应用程序。

我这样使用它:

  mInstalledApplicationList = new ArrayList<App>();

        mInstalledApplicationList.addAll(ApplicationList.mInstalledAppList);
        Log.e(TAG, "Total Installed App Size : " + ApplicationList.mInstalledAppList.size());

        mInstalledApplicationList.removeAll(ApplicationList.mSelecteddAppList);
        Log.e(TAG, "Filtered Installed App Size : " + mInstalledApplicationList.size());

我的APP收藏类是:

import java.util.Comparator;
import java.util.List;

import android.content.ComponentName;
import android.content.Intent;
import android.graphics.drawable.Drawable;

public class App implements Comparable<App> {

    private String title;

    private String packageName;

    private String versionName;

    private int versionCode;

    private String app_class;

    private String installDate;

    private int installDateMilli;

    private List<String> appPermissions;

    private Drawable mIcon;

    public Intent intent;

    // ordinary getters and setters

    public Drawable getmIcon() {
        return mIcon;
    }

    public void setmIcon(Drawable mIcon) {
        this.mIcon = mIcon;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getDateMilli() {
        return installDateMilli;
    }

    public void setDateMilli(int DateMilli) {
        this.installDateMilli = DateMilli;
    }

    public List<String> getAppPermissions() {
        return appPermissions;
    }

    public void setAppPermissions(String appPermission) {
        this.appPermissions.add(appPermission);
    }

    public String getInstallDate() {
        return installDate;
    }

    public void setInstallDate(String installDate) {
        this.installDate = installDate;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }

    public String getAppClass() {
        return app_class;
    }

    public void setAppClass(String app_class) {
        this.app_class = app_class;
    }

    final void setActivity(ComponentName className, int launchFlags) {
        intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(className);
        intent.setFlags(launchFlags);
    }

    @Override
    public int compareTo(App app) {

        return 0;
    }

    public static Comparator<App> AppNameComparator = new Comparator<App>() {

        public int compare(App app1, App app2) {

            String AppName1 = app1.getTitle().toUpperCase();
            String AppName2 = app2.getTitle().toUpperCase();

            // ascending order
            return AppName1.compareTo(AppName2);

            // descending order
            // return fruitName2.compareTo(fruitName1);
        }

    };

    public static Comparator<App> AppDateComparator = new Comparator<App>() {

        public int compare(App app1, App app2) {

            int AppName1 = 0;
            int AppName2 = 0;

            AppName1 = app1.installDateMilli;
            AppName2 = app2.installDateMilli;

            return AppName2 - AppName1;

        }

    };

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof App)) {
            return false;
        }

        App that = (App) o;
        return title.equals(that.title)
                && intent.getComponent().getClassName()
                        .equals(that.intent.getComponent().getClassName());
    }

    @Override
    public int hashCode() {
        int result;
        result = (title != null ? title.hashCode() : 0);
        final String name = intent.getComponent().getClassName();
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}

** 我已经更新了我的应用程序类。这是我的应用类**

解决方案

我通过改变 equals 和 hashCode 方法解决了我的问题,如下所示:

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (!(o instanceof App)) {
        return false;
    }

    App that = (App)o;
    return title.equals(that.title) && intent.getComponent().getClassName().equals(that.intent.getComponent().getClassName()) && packageName.equals(that.packageName);
}

@Override
public int hashCode() {
    int result;
    result = (title != null ? title.hashCode() : 0);
    final String name = intent.getComponent().getClassName();
    int pkg_code = (packageName != null ? packageName.hashCode() : 0);
    result = 31 * result + pkg_code + (name != null ? name.hashCode() : 0);
    return result;
}

谢谢大家的帮助。

4

1 回答 1

2

您需要正确地为您的类实现equals方法。App对您而言,最好的解决方案可能是使用 packageName 作为您应用的唯一标识符:

@Override
public boolean equals(Object o) {

    if (o == this) {
        return true;
    }

    if (!(o instanceof App)) {
        return false;
    }

    App app = (App)o;
    return TextUtils.equals(app.packageName, this.packageName);

}
于 2013-03-07T07:36:03.337 回答