0

我有一个问题,我已经尝试了很多事情,这是最终可能会有所帮助的解决方案。我有一个列表视图,里面装满了数据库数据。用户插入一个任务,任务就会出现在屏幕上。问题是用户还可以插入任务的子任务,这些“新任务”必须显示在深度为 4 的主任务下的列表视图的不同行中。例如:

 -Main_Task1
    -SubTask1
       -subtask1
       -subtask2
    -SubTask2
 -Main_Task2
    -SubTask1
       -subtask1
          -subtask1
 -Main_Task3

这是来自数据库的代码。

    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, " 
    +" check1 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, KEY_TODAY},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_TODAY}, 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_TODAY}, KEY_FATHER + " LIKE" + " '"+ fid +" '"+ " AND " + KEY_ROWID + " LIKE " + " '"+ uid +"' ", null, null, null, null);

} 

这是 TasksCursorAdapter 类的代码。

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

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

          int columnIndex = 0;


          mCursor.moveToFirst();
          while(mCursor.moveToNext()) {

              id = mCursor.getString(0);


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


              // Loop through the subCursor
             while(subCursor.moveToNext()) {

                 timeCursor = mDbHelper.fetchSubTask(id, subCursor.getString(columnIndex));// fetching each time the task with the unique id( from the array)
                 //while(timeCursor.moveToNext()){

                  View line = inflater.inflate(R.layout.tasks_row,null);


                  list.addView(line);


              } }

        //  }

使用此代码,我得到一个列表视图,其中在每个项目列表中出现一个新行,每次添加子任务时都包括未正确获取任务。任何想法都可以提供帮助。提前谢谢

4

1 回答 1

0

如果您尝试在 ListView 的每一行下添加子任务,则应该使用ExpandableListView

于 2012-08-21T15:41:56.470 回答