2013-03-10 161 views
0

我需要您從MySQL數據庫獲取有關retreive數據的幫助。我不能將字符串添加到數組列表。 我的名單宣佈如何將字符串轉換爲ArrayList

ArrayList<HandleListReport> reportList = new ArrayList<HandleListReport>(); 

這裏我的代碼

report_data = json.getJSONArray(TAG_REPORT_DATA); 

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

         //storing variable 
         JSONObject c = report_data.getJSONObject(i); 
         String reportID = c.getString(TAG_REPORT_ID); 
         String userID = c.getString(TAG_UID); 
         String projectName = c.getString(TAG_PROJECT_NAME); 
         String localWork = c.getString(TAG_LOCATION); 
         String timeStart = c.getString(TAG_TIME_START); 
         String timeEnd = c.getString(TAG_TIME_END); 
         Log.d(TAG_LOCATION, localWork); 
         // add data to Arraylist 
         reportList.add(new HandleListReport(reportID, userID, projectName, localWork, timeStart, timeEnd)); 

我的問題,我的listReport缺少字符串數據。 Logcat show reportList ---> []

感謝您的回答!

+0

你在哪裏聲明reportList。寫在這裏,還有你面臨的問題是什麼? – stinepike 2013-03-10 04:34:40

+0

我的logcat在reportList上拋出[] – Ply 2013-03-10 04:39:45

+0

在使用它們創建新的'HandleListReport'對象之前,您是否調試過代碼以查找變量的值 'reportID,userID,projectName,localWork,timeStart,timeEnd'? – 2013-03-10 04:46:47

回答

0

創建字符串的列表

List<String> list ...... 

list.add(String) 

然後list.toArray()方法

http://docs.oracle.com/javase/6/docs/api/java/util/List .html#toArray(T [])

0

它似乎你正試圖將對象的HandleListReport存儲在arraylist中。在這種情況下使用以下方法來啓動

ArrayList<HandleListReport> reportList = new ArrayList<HandleListReport>(); 
+0

它似乎沒有工作,reportList仍然爲空。 – Ply 2013-03-10 04:51:22

+0

你是從哪裏發起arraylist?在oncreate()中啓動它。並在結束循環後檢查reportList.size() – stinepike 2013-03-10 05:02:57

+1

它不必在'onCreate()'。只要它是一個成員變量,那麼它應該沒問題 – codeMagic 2013-03-10 05:04:11

0

似乎變量report_data的長度爲0,所以元素不會添加名單。