0

I'm trying to compare two lists. First list has values from switches of class1 and the other list I'm trying to retreive it from a site (class2). I'm trying to compare those two lists but if statement returns no. For example I'm checking the soy switch and when I scan a barcode the fetched list has soy in it but text returns no. Help me please!

Here is the code that I wrote. Class1

   public ArrayList<String>  soy2= new ArrayList<String>();
    Switch soy;
.....................

 if (soy.isChecked()) {

           checked();
     }
      else {
            textView.setText("blahblah");

      }


        public void checked() {

            soy2.add("Soy");
            soy2.add("Σόγια");
            soy2.add("soja");
            soy2.add("Soybeans");
            soy2.add("soybeans");
            soy2.add("en:soybeans");
           //checkedAllergens.add(soy2);


            }

        public  ArrayList<String>  getList() {
            return soy2;
        }

Class2


public Manage checkd;
         String fetchedAllergens=new String();
    List<String> fetchedAllergensList = new ArrayList<String>();
    .......................

     public void test() {
        checkd = new Manage();

       ArrayList<String> list = checkd.getList();
      System.out.println(list);
    System.out.println(fetchedAllergensList);

         if(fetchedAllergensList.contains(list))
        {


            testtxt.setText("yes");
        }
        else
        {

            testtxt.setText("no");
        }

//Test method is calling by a click listener and i get the list from class 2 with the code below



        protected String doInBackground(String... strings) {

            final String barcode = strings[0];
            @Nullable
            String allergens = null;
            try {
                final String jsonStr = Jsoup.connect(
                        "https://world.openfoodfacts.org/api/v0/product/" + barcode + ".json")
                        .ignoreContentType(true)
                        .execute()
                        .body();

                final JSONObject jsonObj = new JSONObject(jsonStr);
                if (jsonObj.has("product")) {
                    JSONObject productNode = jsonObj.getJSONObject("product");

                    allergens = productNode.getString("allergens");

                }
            } catch (IOException | JSONException e) {
                e.printStackTrace();
            }

            fetchedAllergens=allergens;
            fetchedAllergensList = Arrays.asList(fetchedAllergens.split(","));
            System.out.print(fetchedAllergensList);
            return allergens;
        }
Erythrozyt
  • 802
  • 6
  • 21
Debbie Mp
  • 163
  • 1
  • 11

1 Answers1

0

Here if(fetchedAllergensList.contains(list))

.contains() is expecting an element of your list not an arraylist

ex.

fetchedAllergensList.contains("Soy")

If you want to compare two list, try this

listA.containsAll(listB) && listB.containsAll(listA)

compare lists if equal

To check if list2 contains element that is also present in list1

public boolean elementExist(ArrayList<String> list1, ArrayList<String> list2) {
   for (int i= 0; i < list2.length() i++) {
       if (list1.contains(list2[i])) {
        // element list2[i] exist in list1
        return true;
       }
   }
   return false;
}

This method will return true if list2 has element that is also in list1

elbert rivas
  • 1,464
  • 1
  • 17
  • 15