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。 這是我的代碼。謝謝你提前如何保存返回遊標的數據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);
}
這裏是我的代碼@Sam。主要任務出現在列表中,我想要做的事情是獲取每個任務的子任務(多個級別,每個子任務都可以有子任務)並以列表的形式顯示它們(創建一個特定的文本視圖,其中包含每個任務的詳細信息任務每次任務有一個子任務)。提前感謝您的任何幫助或想法。
嘿嘿,你忘了代碼... – Sam 2012-07-13 17:03:06
我的猜測是你的'mCursor.getCount()'= 0。我會嘗試檢查以確保你的光標正在獲得所需的數據。 – BlackHatSamurai 2012-07-13 17:10:19
你從哪裏得到例外? – Jrom 2012-07-13 17:31:54