如何保存返回Android游标的数据

时间:2012-07-13 17:02:32

标签: 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);

}

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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

一个主要问题是你试图使用一个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

<强>加成

我猜测了你要做的事情并修改了你的代码。但是我没有看到你在哪里使用你所获得的第三个光标,所以这不会是完美的,但也许你会看到你出错的地方:

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

希望有所帮助!