2012-03-14 27 views
-1

我一直在尋找/做不同的需要低於該代碼的:問題與陣列經由環狀的查詢生成導致

public String[] getData() { 

String[] columns = new String[]{ app_name, app_location, app_dateTime}; 
Cursor c = myDatabase.query(app_table, columns, null, null, null, null, null); 

int iName = c.getColumnIndex(app_name); 
int iLoc = c.getColumnIndex(app_location); 
int iDate = c.getColumnIndex(app_dateTime); 


String[] appointments = new String[c.getColumnCount()]; 
int j = 0; 
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
    appointments[j] = " Appointment Name: " + c.getString(iName) + " \n Location: " + c.getString(iLoc) + " \n Date: " + c.getString(iDate) + " \n\n "; 
    j++; 
    } 
return appointments; 

    } 
} 

基本上這個碼的目標是讓我的查詢結果被饋送到一個數組和然後通過代碼的「返回約會」部分在另一個類的ListView中訪問。出於某種原因,我的代碼似乎不工作,我不能完全把我的手指放在它上面,我嘗試過在循環中循環我已經試着淡化代碼,然後展開我可以工作的東西,但沒有一個是給予的我一個解決方案。

回答

0

我很抱歉,謝謝提交答案的人,我已經弄清楚問題所在,它是在我的另一個java類中。上面列出的循環是完全有效的,所以如果有人需要找到一段代碼來完成上面提到的問題,那麼就不要再看了。

1

我認爲與其

String[] appointments = new String[c.getColumnCount()]; 

你需要使用

String[] appointments = new String[c.getCount()]; 

getColumnCount()返回列的數量(這是不斷3 -name,位置,dateTime-),你需要那裏的行數。

+0

我的歉意,我沒有提到,我也試過改變 String []約會=新字符串[c.getCount()]; 至: String []約會=新字符串[99]; 爲了試驗和錯誤。我相信這個錯誤是在for循環本身的某個地方,但我無法找到它。 – 2012-03-14 12:06:34