我有一个问题,我已经尝试了很多东西,这是最终可能有帮助的解决方案。我有一个listview,里面装满了数据库数据。用户插入任务,任务显示在屏幕上。问题是用户还可以插入任务的子任务和这些"新任务"必须在主要任务下以深度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);
} }
// }
使用此代码,我得到一个列表视图,其中每个项目列表中都会出现一个新行,每次添加一个子任务包括未正确获取任务。任何想法都可以帮助。提前感谢