我有以下活动,它是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)中,我进行了以下调用以获取活动
Calculation_getInventory.getInstance().getInventory();
我的问题是如何将CalculationList从Calculation_getInventory传递给Calculation?
答案 0 :(得分:2)
不完全确定你想在这里实现什么,但是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;
}
答案 1 :(得分:1)
您的getInventory()
没有返回任何内容。
设置为返回值ArrayList
并让它返回列表。
示例强>
你有:
public void getStuff() {
int ret = 6;
}
将其转换为:
public int getStuff() {
int ret = 6;
return ret;
}
在您的其他课程中,您可以在课程对象上调用getStuff()
,如下所示:
int stuff = YourClass.getInstance().getStuff();
答案 2 :(得分:0)
只需为Calculation_getInventory类创建一个InventoryList作为全局,并添加一个getter settter方法。
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");
}
}
现在只需使用Calculation_getInventory对象
调用此方法Calculation_getInventory i=Calculation_getInventory.getInstance().getInventory();
List list=i.getInventoryList();