0

我有一个包含 45 个不同条目的 SQLite 数据库,每个条目都有:

public static final String TABLE = "Table";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_HOUR = "hour";
public static final String COLUMN_WEEK = "week";
public static final String COLUMN_DAY = "day";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_COLOUR = "colour";
public static final String COLUMN_ROOM = "room";

现在我想全部读出来。我通过以下方式执行此操作:

public Cursor fetchAllSubject(){
    Cursor mCursor = database.query(true, TABLE, new String[] {
            COLUMN_ID, COLUMN_HOUR, COLUMN_WEEK, COLUMN_DAY, COLUMN_NAME, COLUMN_DESCRIPTION, COLUMN_COLOUR, COLUMN_ROOM},null
            , null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

在另一堂课中,我有这段代码要全部读出来:

dao = new DAO(this);
Cursor subjectList = dao.fetchAllSubject();

现在我希望每个条目都有一个 ID、Hour、week、...的数组,但我不知道该怎么做。我的第一次尝试如下:

ArrayList<String> mo1h = new ArrayList<String>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM)));     
     subjectList.moveToNext();
}

但是一切都在 mo1h 中,我不知道如何划分它。最好的办法是每个都有一个 String[] 。有人有想法吗?谢谢!

4

2 回答 2

4

您可以在Bean 类上创建,然后创建一个ArrayList(Collection 类)

public class Bean
{
   public Bean();
   String id, hour, week, day, name, description, color, room;
}

现在创建 Bean 列表

ArrayList<Bean> mo1h = new ArrayList<Bean>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
Bean b = new Bean();
   b.id = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
   b.hour =subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
   ...
   ...
   // all your column
   mo1h.add(b);
}
于 2012-06-05T18:20:04.123 回答
2

为什么不继续你的策略,而是使用 String[] 的 ArrayList:

ArrayList<String[]> mo1h = new ArrayList<String[]>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
     String[] toUse = new String[8];
     toUse[0] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
     toUse[1] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
     toUse[2] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK));
     toUse[3] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY));
     toUse[4] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME));
     toUse[5] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION));
     toUse[6] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR));
     toUse[7] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM)); 
     mo1h.add(toUse);    
     subjectList.moveToNext();
}
于 2012-06-05T18:23:48.767 回答