I have two classes Class1=Manage and Class2=Scan. I want to get the list from manage and check it with a method in Scan. Here is the code.
The result that i get is "no" and with debugging i saw that list is empty but i don't know why.

Manage.class

  private  ArrayList<String> soy2= new ArrayList<String>();

  ....
   public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

        //12-1-2019
        if (soy.isChecked()) {
            soy1="yes";

           //checked();
            soy2.add("Soy");
            soy2.add("Σόγια");
            soy2.add("soja");
            soy2.add("Soybeans");
            soy2.add("soybeans");
            soy2.add("en:soybeans");
            System.out.println(soy2);
//            //textView.setText("Soy");
//            textView.setText("Soy");
//            System.out.println(soy2);
//            System.out.println(checkedAllergens);
//
        }
        else {
            soy1="no";
            //soy2.remove("Soy");
            soy2.remove("Soy");
            soy2.remove("Σόγια");
            soy2.remove("soja");
            soy2.remove("Soybeans");
            soy2.remove("soybeans");
            soy2.remove("en:soybeans");
            System.out.println(soy2);
        }

   }

            //set value
    public void setArrList (ArrayList soy2){

        this.soy2=soy2;
    }
        public ArrayList<String> getList() {
        System.out.println(soy2);
            return this.soy2;
        }

Scan.class

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

  //test() method is calling on a click listener
  //i get fetchedAllergensList from another click listener
   public void test() {

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

    }
    public boolean ElementExist(ArrayList<String> list1, List<String> list2) {
        for (int i= 0; i<list2.size(); i++) {
            if (list1.contains(list2.get(i))) {
                // element list2[i] exist in list1
                testtxt.setText("yes");
                return true;

            }
        }
        testtxt.setText("no");
        return false;
    }

I can't see any obvious errors in that code, but to go any further we would need to understand the sequence of events that make up your test case.

Make your list static and then populate it in Mange class. Now you will be able to access this List in your scan class.

commented: Just make everything static public, none of that OO nonsense -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.