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

while ループを使用してみてください。エラーは確かに for ループにあります。

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

このループでは、いくつの要素が存在するかは気にしません。それでも outOfBound 例外が発生する場合は、行を正しくフェッチしていません。

于 2012-07-13T17:11:36.423 に答える
0

大きな問題の 1 つは、2 つまたは 3 つの作業に対して 1 つの Cursor を使用しようとしているということです...

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

これは新しい Cursor である必要があります (適切な参照を変更します):

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

添加

あなたがやろうとしていることを推測し、コードを修正しました。しかし、取得した 3 番目の Cursor を使用している場所がわからないため、これは完璧ではありませんが、どこが間違っているかがわかります。

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

それが役立つことを願っています!

于 2012-07-13T17:14:03.187 に答える