2012-07-13 35 views
0
private static final String DATABASE_CREATE = 
    "create table tasks (_id integer primary key autoincrement, " 

    + "title text not null," 
    +"location text not null, " 
    + "body text not null , " 
    +" goaldate text not null , " 
    +" absolutedate text not null , " 
    +"currentdate text not null , " 
    +"prio text not null, " 
    +"done text not null, " 
    +"category text not null, " 
    +"timespent text not null, " 
    +"pososto text not null," 
    +"father text not null);"; 

// fetch all tasks(tasks and subtasks) 
public Cursor fetchTasks() { 

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
      KEY_BODY,KEY_location , KEY_GOAL_DATE,KEY_ABSOLUTE_DATE, KEY_DATE_CURRENT ,KEY_PRIO,KEY_DONE, KEY_CATEGORY,KEY_TIME_SPEND,KEY_POSOSTO, KEY_FATHER},null, null, null, null, null); 

} 

//fetch subtasks of a task, given the father's key 
public Cursor fetchSubTasks(String id) { 

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
      KEY_BODY,KEY_location , KEY_GOAL_DATE,KEY_ABSOLUTE_DATE, KEY_DATE_CURRENT ,KEY_PRIO,KEY_DONE, KEY_CATEGORY,KEY_TIME_SPEND,KEY_POSOSTO, KEY_FATHER}, KEY_FATHER + " LIKE" + "'"+ id+"'", null, null, null, null); 

} 

//fetch a subtask 
public Cursor fetchSubTask(String fid,String uid) { // tropopoihsh ths methodou wste sthn arxiki othoni na fainontai mono oi tasks 

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
      KEY_BODY,KEY_location , KEY_GOAL_DATE,KEY_ABSOLUTE_DATE, KEY_DATE_CURRENT ,KEY_PRIO,KEY_DONE, KEY_CATEGORY,KEY_TIME_SPEND,KEY_POSOSTO, KEY_FATHER}, KEY_FATHER + " LIKE" + " '"+ fid +" '"+ " AND " + KEY_ROWID + " LIKE " + " '"+ uid +"' ", null, null, null, null); 

} 

      mCursor= mDbHelper.fetchTasks();//fetching all the tasks 
      int numberoftasks= mCursor.getCount(); 
      this.mCursor.moveToFirst(); 
      int n,m; 
      String id, idf; 

      int columnIndex= 0; 
      for(n=0;n<=numberoftasks;n++){ 

       id=this.mCursor.getString(0);//unique id of the current task 

       mCursor=mDbHelper.fetchSubTasks(id);// fetching the subtasks with father_id=id 

       String[] my = new String[mCursor.getCount()]; 

       if (mCursor.moveToFirst()) 
       {      
        for (int i = 0; i < mCursor.getCount(); i++) 
        { 
         my[i] = mCursor.getString(columnIndex); 
         mCursor.moveToNext(); 
        }   
       } 

      m=0; 


      int subtasks=mCursor.getCount(); 

      int j; 
      for(j=0;j<=subtasks;j++){ 

        mCursor=mDbHelper.fetchSubTask(id, my[j]);// fetching each time the task with the unique id(from the array) 
                  //and father_id=id 

        LinearLayout list = (LinearLayout)v.findViewById(R.id.linear); // fill a textview of the list with one more textview 
        list.removeAllViews(); 
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

       // for (Music music : albums.get(position).musics) { 
         View line = inflater.inflate(R.layout.tasks_row, null); 

         /* nested list's stuff */ 

         list.addView(line); 
        } 

       m++; 
      } 

      this.mCursor.moveToNext(); 
     // } 

我有一個數據庫,其中有一張桌子,裏面裝滿了任務。每個任務都有唯一的ID和父親的ID以實現嵌套。我的問題是,我無法正確保存遊標返回的數據,並得到一個arrayOutOfboundsException。 這是我的代碼。謝謝你提前如何保存返回遊標的數據Android


 private static final String DATABASE_CREATE = 
    "create table tasks (_id integer primary key autoincrement, " 

    + "title text not null," 
    +"location text not null, " 
    + "body text not null , " 
    +" goaldate text not null , " 
    +" absolutedate text not null , " 
    +"currentdate text not null , " 
    +"prio text not null, " 
    +"done text not null, " 
    +"category text not null, " 
    +"timespent text not null, " 
    +"pososto text not null," 
    +"father text not null);"; 

    // fetch all tasks(tasks and subtasks) 
public Cursor fetchTasks() { 

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
      KEY_BODY,KEY_location , KEY_GOAL_DATE,KEY_ABSOLUTE_DATE, KEY_DATE_CURRENT ,KEY_PRIO,KEY_DONE, KEY_CATEGORY,KEY_TIME_SPEND,KEY_POSOSTO, KEY_FATHER},null, null, null, null, null); 

} 

//fetch subtasks of a task, given the father's key 
public Cursor fetchSubTasks(String id) { 

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
      KEY_BODY,KEY_location , KEY_GOAL_DATE,KEY_ABSOLUTE_DATE, KEY_DATE_CURRENT ,KEY_PRIO,KEY_DONE, KEY_CATEGORY,KEY_TIME_SPEND,KEY_POSOSTO, KEY_FATHER}, KEY_FATHER + " LIKE" + "'"+ id+"'", null, null, null, null); 

} 

//fetch a subtask 
public Cursor fetchSubTask(String fid,String uid) { // tropopoihsh ths methodou wste sthn arxiki othoni na fainontai mono oi tasks 

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
      KEY_BODY,KEY_location , KEY_GOAL_DATE,KEY_ABSOLUTE_DATE, KEY_DATE_CURRENT ,KEY_PRIO,KEY_DONE, KEY_CATEGORY,KEY_TIME_SPEND,KEY_POSOSTO, KEY_FATHER}, KEY_FATHER + " LIKE" + " '"+ fid +" '"+ " AND " + KEY_ROWID + " LIKE " + " '"+ uid +"' ", null, null, null, null); 

} 

這裏是我的代碼@Sam。主要任務出現在列表中,我想要做的事情是獲取每個任務的子任務(多個級別,每個子任務都可以有子任務)並以列表的形式顯示它們(創建一個特定的文本視圖,其中包含每個任務的詳細信息任務每次任務有一個子任務)。提前感謝您的任何幫助或想法。

+7

嘿嘿,你忘了代碼... – Sam 2012-07-13 17:03:06

+0

我的猜測是你的'mCursor.getCount()'= 0。我會嘗試檢查以確保你的光標正在獲得所需的數據。 – BlackHatSamurai 2012-07-13 17:10:19

+0

你從哪裏得到例外? – Jrom 2012-07-13 17:31:54

回答

0

嘗試使用while循環,錯誤是在for循環肯定的:

while(cursor.moveToNext()){ 
     //do your thing 
} 

這個循環你不在乎的元素存在多少。如果這仍然給你一個outOfBound異常,那麼你不會正確地獲取這些行。

+0

我改變了你的答案外循環,我仍然得到例外。我認爲這個問題基本上是在數組和存儲數據的方式上。我也會檢查提取。 – user1347280 2012-07-13 17:25:59

0

的一個主要問題是,你要使用一個光標爲兩個或三個工作...

mCursor=mDbHelper.fetchSubTasks(id);// fetching the subtasks with father_id=id 

這需要一個新的光標(和更改相應的參考):

Cursor cursorId = mDbHelper.fetchSubTasks(id);// fetching the subtasks with father_id=id 

否則,你的for循環或光標訪問外將在這裏跑出界:

for(n=0;n<=numberoftasks;n++){ 
    id=this.mCursor.getString(0);//unique id of the current task 

加法

我猜測你想要做什麼並修改你的代碼。但我不明白,你正在使用你獲取的第三光標,所以這不會是完美的,但也許你會看到你要去哪裏錯了:

// You only need to find this layout once and create one LayoutInflater 
LinearLayout list = (LinearLayout)v.findViewById(R.id.linear); // fill a textview of the list with one more textview 
list.removeAllViews(); 
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

Cursor mCursor = mDbHelper.fetchTasks();//fetching all the tasks 
long id; 

int columnIndex = 0; 
while(mCursor.moveToNext()) { 
    // If this is a SQLite id then use: 
    id = mCursor.getLong(0); 
    //id = this.mCursor.getString(0);//unique id of the current task 

    Cursor subCursor = mDbHelper.fetchSubTasks(id);// fetching the subtasks with father_id=id 

    // Loop through the subCursor, I didn't see any need to save the temporary value in an array 
    while(subCursor.moveToNext()) { 
     Cursor timeCursor = mDbHelper.fetchSubTask(id, subCursor.getString(columnIndex));// fetching each time the task with the unique id(from the array) 
     //and father_id=id 

     // for (Music music : albums.get(position).musics) { 
     View line = inflater.inflate(R.layout.tasks_row, null); 

     /* nested list's stuff */ 
     list.addView(line); 
    } 
} 

希望幫助!

+0

我不認爲每次我抓取時都需要新的遊標。遊標需要一個新的結果集。我錯了嗎? – user1347280 2012-07-13 17:28:44

+0

在'for(n = 0 ...)'的第一行中,您嘗試一次一個地循環所有任務,但是您更改了第二行中的Cursor。因此,當您循環回到'對於(n = 0 ...)來讀第二個任務,你現在指着一個非常不同的光標... – Sam 2012-07-13 17:40:32

+0

It.does,但你仍然在使用第一個光標來驅動循環,當你改變遮陽板你改變了循環。 – Barak 2012-07-13 17:43:47

相關問題