Sjharrison
Sjharrison

Reputation: 729

Android - Retrieve ArrayList from other Activity

I have the following Activity which is an independent class from the MainActivity

public class Calculation_getInventory {
SQLiteDatabase db;
private static Calculation_getInventory _instance = null;
public static Calculation_getInventory getInstance(){
    if(_instance == null)
        _instance= new Calculation_getInventory();
    return _instance;
  }
  private Calculation_getInventory(){} 

  public void getInventory() {
    db = SQLiteDatabase.openDatabase("/data/data/com.sjhdevelopment.shaunharrison.myejuiceapp/databases/EJuiceData.db", null, 0);
    Cursor c = db.rawQuery("SELECT * FROM Inventory", null);
    List InventoryList = new ArrayList();
    if (c.moveToFirst()) {
        do {
            Double amountLeft = Double.parseDouble(c.getString(4));
            if (amountLeft != 0) {
                InventoryList.add(c.getString(1));
            }
        } while (c.moveToNext());
    }
    java.util.Collections.sort(InventoryList);
    InventoryList.add(0, "No Selection");

  }
}

In my calculation class (MainActivity) I have made the following call to get the activity

Calculation_getInventory.getInstance().getInventory();

My question is how do I pass the ArrayList from Calculation_getInventory over to Calculation?

Upvotes: 0

Views: 60

Answers (3)

Angad Tiwari
Angad Tiwari

Reputation: 1768

just create a InventoryList as global for Calculation_getInventory class and add a getter settter method..

public class Calculation_getInventory {
List InventoryList = new ArrayList(); // create global var of list
SQLiteDatabase db;
private static Calculation_getInventory _instance = null;

// call this method to get arraylist /list of inventory
public List getInventoryList()
{
    if(InventoryList==null)
          return new ArrayList();
    InventoryList;
}

public static Calculation_getInventory getInstance(){
if(_instance == null)
    _instance= new Calculation_getInventory();
return _instance;
}
private Calculation_getInventory(){} 

public void getInventory() {
db =    SQLiteDatabase.openDatabase("/data/data/com.sjhdevelopment.shaunharrison.myejuiceapp/databases/EJuiceData.db", null, 0);
Cursor c = db.rawQuery("SELECT * FROM Inventory", null);
//List InventoryList = new ArrayList(); comment this line and declare globally
if (c.moveToFirst()) {
    do {
        Double amountLeft = Double.parseDouble(c.getString(4));
        if (amountLeft != 0) {
            InventoryList.add(c.getString(1));
        }
    } while (c.moveToNext());
}
java.util.Collections.sort(InventoryList);
InventoryList.add(0, "No Selection");

 }
}

now just call this method using Calculation_getInventory object

Calculation_getInventory i=Calculation_getInventory.getInstance().getInventory();
List list=i.getInventoryList(); 

Upvotes: 0

showp1984
showp1984

Reputation: 378

Your getInventory() isn't returning anything. Set as it's return value ArrayList and let it return your list.

Example

You have:

public void getStuff() {
    int ret = 6;
}

Convert it to this:

public int getStuff() {
    int ret = 6;
    return ret;
}

In your other class you can then call getStuff() on an object of your class like this:

int stuff = YourClass.getInstance().getStuff();

Upvotes: 1

SuperFrog
SuperFrog

Reputation: 7674

Not totally sure what you want to achieve here, but getInventory() should return the value, instead of:

public void getInventory()

It should be:

public ArrayList getInventory()

And should return InventoryList

public ArrayList getInventory() {
    db = SQLiteDatabase.openDatabase("/data/data/com.sjhdevelopment.shaunharrison.myejuiceapp/databases/EJuiceData.db", null, 0);
    Cursor c = db.rawQuery("SELECT * FROM Inventory", null);
    List InventoryList = new ArrayList();
    if (c.moveToFirst()) {
        do {
            Double amountLeft = Double.parseDouble(c.getString(4));
            if (amountLeft != 0) {
                InventoryList.add(c.getString(1));
            }
        } while (c.moveToNext());
    }
    java.util.Collections.sort(InventoryList);
    InventoryList.add(0, "No Selection");

    return InventoryList;
}

Upvotes: 2

Related Questions