如何比较android中两个arraylists的值?

时间:2017-07-11 04:21:46

标签: android listview

我有一个应用程序,其中包含两个主要是“A”和“B”的活动,活动“A”包含在活动“A”中填充listview的arraylist,而活动“B”也包含在活动“B”中填充listview的arraylist 。我想要的是来自Activity“B”的arraylist的“packagename”值,并与来自Activity“A”的arraylist的“packagename”值进行比较。我想要的是,如果两个“packagename”值相同,那么我想使用continue关键字跳转。

 private List<AppList> getInstalledApps() {
    List<AppList> res = new ArrayList<AppList>();
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
    for (int i = 0; i < packs.size(); i++) {
        PackageInfo p = packs.get(i);
        if ((!isSystemPackage(p))) {
            boolean isWhiteList = false;
            if (whiteListModels!=null) {
                for (int j = 0; j< whiteListModels.size(); j++) {
                    model = whiteListModels.get(j);
                    Log.e(TAG,"p*****"+model.getPackName());
                    if (p.applicationInfo.loadLabel(getPackageManager()).toString().equalsIgnoreCase(model.getPackName())) {
                        // This package is whitlist package
                        isWhiteList = true;
                    }

                }
            }
            // We don't need to add white list app in the list
            if (isWhiteList) {
                continue;
            }

            String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
            Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
            String packageName = p.applicationInfo.packageName;

            Log.e(TAG, "package name::" + packageName);
            Log.e(TAG, "icon name::" + icon);
            res.add(new AppList(appName, icon, packageName));
        }
    }
    return res;
}

4 个答案:

答案 0 :(得分:1)

考虑使用ArrayList类的.contains方法来查找对象是否存在于另一个对象中。

ArrayList<MyClass>List_1=new ArrayList<>();
ArrayList<MyClass>List_2=new ArrayList<>();

Iterator<MyClass> iterator=List_1.iterator();

while(iterator.hasNext()){
    MyClass obj=iterator.next();
    if(List_2.contains(obj)){
      //found
    }
}

答案 1 :(得分:1)

你可以这样试试,

    private List<AppList> getInstalledApps() {
        List<AppList> res = new ArrayList<AppList>();
        List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < packs.size(); i++) {
            PackageInfo p = packs.get(i);
            if ((!isSystemPackage(p))) {
                boolean isWhiteList = false;

                if (whiteListModels != null) {
                    for (int j = 0; j < whiteListModels.size(); j++) {
                        WhiteListModel model = whiteListModels.get(j);
//                        s = model.getPackName();
//                        Log.e(TAG, "PackageName::" + s);

                        if (p.applicationInfo.packageName.equalsIgnoreCase(model.getPackName())) {
                            // This package is whitlist package
                            isWhiteList = true;

                            break;
                        }
                    }
                }

                // We don't need to add white list app in the list
                if (isWhiteList) {
                    continue;
                }
                // We should compare with package name not label to ignore the white list app
//                if (p.applicationInfo.loadLabel(getPackageManager()).toString().equalsIgnoreCase(s)) {
//                    continue;
//                }

                String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
                Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
                String packageName = p.applicationInfo.packageName;

                Log.e(TAG, "package name::" + packageName);
                Log.e(TAG, "icon name::" + icon);
                res.add(new AppList(appName, icon, packageName));
            }
        }
        return res;
    }

答案 2 :(得分:0)

您可以使用内部循环来检查来自arrayList2的Person的id是否对应于arrayList1中的任何Person id。如果找到某人,你需要一个标记来标记。

ArrayList<Integer> results = new ArrayList<>();

// Loop arrayList2 items
for (Person person2 : arrayList2) {
    // Loop arrayList1 items
    boolean found = false;
    for (Person person1 : arrayList1) {
        if (person2.id == person1.id) {
            found = true;
        }
    }
    if (!found) {
        results.add(person2.id);
    }
}

希望这有帮助!!!

答案 3 :(得分:0)

您需要使用Predicate来解决您的问题。 让我举个例子:

  1. 创建一个返回布尔值的方法,该值检查您的包是否列为白名单。

    public boolean isWhitelistedPackage(String mPackageName) {
       if (getPacakageNames() == null) { // This should be your Activity B's list items
          return null;
       }
       // Create collection
       Collection<Model> whiteListModels = 
       Collections2.filter(getPacakageNames(), isPackageSafe(mPackageName));
       List<Model> modelList = new ArrayList<>();
       imageItemList.addAll(whiteListModels);
       Model model = modelList.get(0); // As we are comparing single item, this would be always 0th position to check. 
       boolean value = mPackageName.equalsIgnoreCase(model.getPackName())
       return value;
    }
    
  2. 创建上述方法中使用的谓词isPackageSafe。

    private Predicate<Model> isPackageSafe(String mPackageName) {
        return new Predicate<Model>() {
            @Override
            public boolean apply(Model modelItem) {
                return modelItem.getPackName().equalsIgnoreCase(mPackageName);
            }
        };
    }
    
  3. 感谢。