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。这是我的代码。先感谢您


     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。主要任务出现在列表中,我想做的事情是获取每个任务的子任务(多个级别,每个子任务可以有子任务)并将它们显示在一种列表中。(创建一个特定的文本视图,其中包含每个任务的详细信息每次任务有子任务时的任务)。提前感谢您的任何帮助或想法。

4

2 回答 2

0

One major problem is that you are trying to use one Cursor for the work of two or three...

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

This needs to be a new Cursor (and change the appropriate references):

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

Otherwise your outer for-loop or cursor access will run out of bounds here:

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

Addition

I took a guess at what you are trying to do and modified your code. But I don't see where you are using the third Cursor that you fetched, so this won't be perfect but maybe you will see where you are going wrong:

// 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);
    }
}

Hope that helps!

于 2012-07-13T17:14:03.187 回答
0

尝试使用while循环,错误肯定在for循环中:

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

使用此循环,您不必关心存在多少元素。如果这仍然给您一个 outOfBound 异常,那么您没有正确获取行。

于 2012-07-13T17:11:36.423 回答