2015-10-14 94 views
0

檢索的ArrayList我有以下的活動是從在MainActivityAndroid的 - 其他活動

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(); 

我的問題是我如何從Calculation_getInventory傳遞ArrayList到計算?

回答

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; 
} 
+0

嗨調用該方法,它的原因是,我的MainActivity是相當全面,它口口聲聲說它的跳幀所有的時間,所以我試圖從主要活動中獲取一些較重的代碼以加快它的速度 – Sjharrison

+0

也許你應該考慮在另一個線程上執行一些東西,而不是使用異步等。 –

+0

因此,我在上面做的事情將沒有什麼區別?對不起,我很新的android! – Sjharrison

1

您的getInventory()未返回任何內容。 設置爲返回值ArrayList並讓它返回您的列表。

您有:

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

將其轉換爲這樣的:

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

在你的其他類,那麼你可以調用getStuff()你的類的對象,這樣的:

int stuff = YourClass.getInstance().getStuff(); 
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(); 
相關問題