Reputation: 813
My first list1 and my list show the right values but the second list1 shows nothing:
public class IcsList {
ArrayList<String> list1= new ArrayList<String>();
public ArrayList<String> list(ArrayList list){
this.list1= list;
Log.i("list1", "" + list1);
Log.i("list", "" + list);
setList();
return list;
}
public ArrayList setList(){
Log.i("list1", "" + list1);
return list1;
}
Here is my logcat:
04-27 21:45:10.094 31548-31597/com.parse.starter I/list﹕ [B Soccer @ Liberty, Apr 28 2015 5:30 PM, KGHS Athletics, G Soccer v Liberty (home), Apr 28 2015 5:30 PM, KGHS Athletics
04-27 21:45:10.133 31548-31597/com.parse.starter I/list1﹕ [B Soccer @ Liberty, Apr 28 2015 5:30 PM, KGHS Athletics, G Soccer v Liberty (home), Apr 28 2015 5:30 PM, KGHS Athletics
04-27 21:45:10.133 31548-31548/com.parse.starter I/list1﹕ []
By the way I'm trying to use this arraylist in another class by adding the string values to an array list in that class. Could setList() be making it empty? And should I instead say "return this.list1". By the way there is an arraylist in another class and I am calling list.(ArrayList). The getter is going to be called in another class to retrieve this arraylist. This class is like a relay between the two.
In my first class where I set the arrayList in the IcsList:
ArrayList<String> arrayList = new ArrayList();
//add to arrayList
IcsList list = new IcsList();
list.list(arrayList);
Class that retrieved from IcsList:
ArrayList<String> arrayList = new ArrayList();
IcsList list = new IcsList();
arrayList= list.setList();
Upvotes: 1
Views: 1187
Reputation: 187
Taylor: based on your last answer for yourself - you have some misunderstandings:
Class IcsList has static list1. It means that all instances share the same ONE list1 ArrayList. So, whoever and whenever uses your class - it is always one the same list1 object.
Even more strange - you initialize this list1 in constructor of the IcsList. Which means list1 is null until you create at least one instance of IcsList. Which is impractical and confusing. Even worse - every new IcsList() will update this list1 with new fresh and empty ArrayList. Which is weird too.
Your last code shows that you create list2 and then immediately override it list1 reference. This has no sense either.
I assume what you want is this:
public class IcsList {
private ArrayList<String> list1;
public IcsList(){
list1 =new ArrayList<String>();
}
public static ArrayList<String> getList1(){
return list1;
}
}
then second one is right (assuming honey is "honey") and third one would be
ArrayList<String> list2= = icsList.getList1(); // this is list1 from existing object icsList
if (list2.contains(honey)) System.out.println("honey is here");
if (icsList.getList1().contains(honey)) System.out.println("honey is still here");
Upvotes: 2
Reputation: 813
For anyone in this predicament my getter-setter:
public class IcsList {
static ArrayList<String> list1;
public IcsList(){
list1 =new ArrayList<String>();}
public static ArrayList<String> getList1(){
return list1;
} }
Where I add to the ArrayList in the IcsList class:
IcsList icsList= new IcsList();
ArrayList<String> arrayList= icsList.getList1();
arrayList.add(event);
arrayList.add(honey);
arrayList.add(happy);
Where I retrieve the ArrayList to use in another class:
ArrayList<String> list2= new ArrayList();
list2= IcsList.getList1();
I would describe this as a "relay system."
Upvotes: 1
Reputation: 719
You didn't provide full comprehensive code. It looks like you call setList() from another place just after you had instantiated your object IcsList.
I see it because if you run your code as it is (assuming adding the rest to be at least compile) then all 3 lines of logcat would be identical. However they all are different, even first and second.
My suggestion is to add specific details details to each output. At least method name or stack trace and you will see the difference - who is calling and when - and there would be no confusion.
Upvotes: 1