方案:我有两个数据列表白名单应用程序详细信息。一个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;
}
}
**我已更新了我的App类。这是我的App类**
解
我通过更改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;
}
谢谢大家的帮助。
答案 0 :(得分: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);
}