gpt4 book ai didi

java - Android - 从其他 Activity 检索 ArrayList

转载 作者:行者123 更新时间:2023-12-01 22:04:06 25 4
gpt4 key购买 nike

我有以下 Activity,它是一个独立于 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");

}
}

在我的计算类(MainActivity)中,我进行了以下调用来获取 Activity

Calculation_getInventory.getInstance().getInventory();

我的问题是如何将 ArrayList 从 Calculation_getInventory 传递到 Calculation?

最佳答案

不完全确定您想在这里实现什么,但 getInventory() 应该返回值,而不是:

public void getInventory()

应该是:

public ArrayList getInventory()

并且应该返回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;
}

关于java - Android - 从其他 Activity 检索 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33120767/

25 4 0