Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Development: Accessing LinkedList of objects (defined in Application class) from Main activity
    text
    copied!<p>Following on from an earlier thread, I had wondered about creating linkedlists and have them be persistent across multiple activities. It was suggested that I extend the application class to do this so I decided to look into it.</p> <p>I have since done this, assuming I haven't made a mess of it, the app should now fill in a linkedlist with some example data that I had hoped to use in the main activity. But I can't seem to access the data, and I'm a bit stumped.</p> <p>Just getting started with android development so sorry if this is a ridiculous question. Anyway here is what i have so far.</p> <p>The extending class <strong>GlobalData</strong></p> <pre><code>package com.example.employeeList; import java.util.LinkedList; import android.app.Application; import android.widget.Toast; import com.example.employeeList.Employee; public class GlobalData extends Application { public LinkedList&lt;Employee&gt; EmployeeList; @Override public void onCreate(){ super.onCreate(); createEmployeeLinkedList(); } public void createEmployeeLinkedList(){ // create a linked list Employee emp1 = new Employee("John"); Employee emp2 = new Employee("Bob"); Employee emp3 = new Employee("Fred"); LinkedList&lt;Employee&gt; EmployeeList = new LinkedList&lt;Employee&gt;(); EmployeeList.add(emp1); EmployeeList.add(emp2); EmployeeList.add(emp3); // The following code works, used for testing to see if linklist was actually being created. // Employee boo = newEmployee(); // Employee boo = EmployeeList.getFirst(); // Toast.makeText(getApplicationContext(), "("+boo.name+")" ,Toast.LENGTH_LONG).show(); } } </code></pre> <p><strong>Employee</strong> class</p> <pre><code>package com.example.employeeList; import android.app.Application; public class Employee extends Application{ public String name; public Employee(String name) { this.name = name; } } </code></pre> <p><strong>MainActivity</strong> class</p> <pre><code>package com.example.employeeList; import android.os.Bundle; import android.app.Activity; import android.widget.TextView; import android.widget.Toast; import com.example.employeeList.Employee; public class MainActivity extends Activity { protected GlobalData globalData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the application instance globalData = (GlobalData)getApplication(); //Would have thought I could just do this but apparently not. Employee boo = globalData.EmployeeList.getFirst(); Toast.makeText(getApplicationContext(), "("+boo.name+")" ,Toast.LENGTH_LONG).show(); } } </code></pre> <p>The offending lines appear to be</p> <pre><code> Employee boo = globalData.EmployeeList.getFirst(); Toast.makeText(getApplicationContext(), "("+boo.name+")" ,Toast.LENGTH_LONG).show(); </code></pre> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload