시나리오 : 두 개의 배열 목록 부엉 애플 리케이션 정보가 있습니다. 하나의 ArrayList에는 모든 응용 프로그램의 컬렉션이 있고 두 번째 ArrayList에는 선택된 응용 프로그램의 컬렉션이 있습니다. 그리고 모든 응용 프로그램 목록에서 선택한 응용 프로그램을 제거해야합니다. 이를 위해 ArrayList.removeAll 함수를 사용했습니다.Android : 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;
}
}
는 ** 난 내 응용 프로그램 클래스를 업데이트했습니다. 당신이 도움을
@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;
}
당신에게 모두 감사 :이 내 응용 프로그램 클래스 **
솔루션
이 같은 등호와 hashCode 방법을 변경하여 내 문제를 해결 한 것입니다.
'compareTo' 구현은 어떻게 생겼습니까? –
@Jon Skeet, 잘 기억한다면 ArrayList는 compareTo가 아닌 객체 비교에 equals를 사용합니다. –
@vmironov : True, true. 그렇다면'equals' 구현은 어떻게 생겼을까요? (내 추측에 따르면 제목에 관한 것입니다 ...) –