2015-08-27 29 views
0

我一直在試圖發送此多維ArrayList到另一個活動很長一段時間了,我已經在StackOverflow上發現了很多似乎相關的答覆,但不幸的是我不能應用任何這些。設置多維ArrayList並傳遞給另一個活動

我認爲在設置數組的方式上可能會出錯。

ArrayList<java.io.Serializable> prodlist = new ArrayList<java.io.Serializable>(); 

for (int i = 0; i < jsonMainArr.length(); i++) { 

    JSONObject childJSONObject = jsonMainArr.getJSONObject(i); 
    String name  = childJSONObject.getString("Product"); 
    String reference = childJSONObject.getString("reference"); 
    String link  = childJSONObject.getString("img"); 
    String qty  = childJSONObject.getString("quantity"); 
    String prodbcode = childJSONObject.getString("bcode"); 

    ArrayList<java.io.Serializable> prod = new ArrayList<java.io.Serializable>(); 
    prod.add(prodbcode); //0 barcode 
    prod.add(name);  //1 name 
    prod.add(reference); //2 ref 
    prod.add(link);  //3 img 
    prod.add(qty);  //4 qty  

    prodlist.add(prod); 

} 

我真的不知道,如果這是prodlist.add(prod)創建這個多維數組的正確途徑。

比傳遞數組我使用:

Intent i = new Intent(getApplicationContext(), PrepareProduct.class); 
i.putExtra("products", prodlist); 
startActivity(i); 

和接收它的另一面:

Bundle extras = getIntent().getExtras(); 
ArrayList productsNameList = extras.getParcelableArrayList("products"); 

但我發現了一個一維陣列,而不是二維的。

我是一名嘗試學習Java的PHP開發人員,而且這些數組正在苦苦掙扎。 謝謝。我知道我可能已經在StackOverflow中實現了這個功能,但我真的無法以我能應用的方式找到它。

+0

你在一維數組中獲得了什麼? – Kun

回答

1

正確的方法來建立一個多維的ArrayList是做

ArrayList<ArrayList<String>> prodlist = new ArrayList<ArrayList<String>>(); 
ArrayList<String> prod = new ArrayList<String>(); 
prod.add(prodbcode); //0 barcode 
prod.add(name);  //1 name 
prod.add(reference); //2 ref 
prod.add(link);  //3 img 
prod.add(qty);  //4 qty  

prodlist.add(prod); 

你的代碼給你一個一分維的ArrayList是因爲產品數的ArrayList是類型的序列化,和ArrayList本身的原因實現Serializable所以編譯,但作爲prodlist只是一個維度,這就是你得到的。

+0

好的......這種語言比PHP技術要好得多,希望我能得到它。感謝您的幫助,現在我以我想要的方式獲取陣列。 –

相關問題