Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - can't store a list in the Application class instance

I'm trying to store a list in the Application class instance as a global variable in one of my Android applications. Below is my Application class code:

public class DefectsApplication extends Application{

private NormalUser normalUser;

private ArrayList<Complaint> complaintList;

public String getTestString() {
    return testString;
}

public void setTestString(String testString) {
    this.testString = testString;
}

private String testString;

public NormalUser getNormalUser() {
    return normalUser;
}

public void setNormalUser(NormalUser normalUser) {

    this.normalUser = normalUser;
}

public ArrayList<Complaint> getComplaintList() {
    return complaintList;
}

public void setComplaintList(ArrayList<Complaint> m_complaints) {
    this.complaintList = complaintList;
}
}

Below is my code which is trying to access the fields from the Application class instance:

DefectsApplication defectsApplication = ((DefectsApplication)getApplicationContext());
defectsApplication.setComplaintList(m_complaints);
defectsApplication.setTestString("urghhhhhhhhh");
ArrayList<Complaint> complaintList = defectsApplication.getComplaintList();
String s = defectsApplication.getTestString();

In the above code, m_complaints is a list of objects. When I try to store a String, it works. But for a list, it doesn't. Please, help me to resolve this issue.

like image 800
Amila Fonseka Avatar asked Dec 28 '25 16:12

Amila Fonseka


1 Answers

Probably, a typo is taking place:

public void setComplaintList(ArrayList<Complaint> m_complaints) {
    this.complaintList = complaintList;
}

You're setting this.complaintList to itself which is initially null. Try

public void setComplaintList(ArrayList<Complaint> m_complaints) {
    this.complaintList = m_complaints;
}
like image 145
Onik Avatar answered Dec 30 '25 06:12

Onik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!