0

I have an app which contain two activities mainly "A" and "B", Activity "A" contains arraylist which populate listview in Activity "A" and Activity "B" also contain arraylist which populate listview in activity "B". What I want get is "packagename" value from arraylist of Activity "B" and compare with "packagename" value from arraylist of Activity "A". What I want is, if both "packagename" values are same then I want to jump using continue keyword.

 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;
}
1
  • Please use proper English, The question doesn't make much sense to me. Commented Jul 11, 2017 at 4:25

4 Answers 4

1

You can try like this,

    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;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Not working value of same package still exist in list1
model.getPackName() is package name or app name..?
If that is package name you should not compare like this, (p.applicationInfo.loadLabel(getPackageManager()).toString().equalsIgnoreCase(model.getPackName())), you should compare with the code in my answer.
1

Consider using the .contains method of the ArrayList class to find whether the object is present in the other.

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

Comments

0

You can use an inner loop, to check if the Person's id from arrayList2 corresponds to any Person id in the arrayList1. You'll need a flag to mark if some Person was found.

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

Hope this helps !!!

1 Comment

I do not want to add ...simple check if let suppose from arraylist1 i got value "a" and compare with arraylist2 if got same value then want to jump from loop
0

you need to use Predicate to resolve your issue. Let me give you an example for it:

  1. Create a method which returns you boolean value which checks your package is white listed or not.

    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. Create predicate isPackageSafe used in above method.

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

Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.