从Cursor获取字符串数组

时间:2012-06-05 18:15:38

标签: android sqlite

我有一个包含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,小时,星期的数组......但我不知道该怎么做。 我的第一次尝试是:

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 []。有人有想法吗? 谢谢!

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分: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();
}