将所有记录调用为String数组

时间:2015-09-12 10:46:03

标签: android arrays sqlite

我需要从TABLE_NOTES获取所有记录,这是我调用所有记录的方法:

public List<NotesFragmentCreate> getAllNotes() {
    List<NotesFragmentCreate> notes = new ArrayList<NotesFragmentCreate>();
    String selectQuery = "SELECT  * FROM " + TABLE_NOTES;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    if (c.moveToFirst()) {
        do {
            NotesFragmentCreate n = new NotesFragmentCreate();
            n.setId(c.getInt(c.getColumnIndex(KEY_ID_NOTES)));
            n.setTitle(c.getString(c.getColumnIndex(KEY_TITLE)));
            n.setCreated(c.getString(c.getColumnIndex(KEY_CREATED)));
            n.setContent(c.getString(c.getColumnIndex(KEY_CONTENT)));

            // adding to note list
            notes.add(n);
        } while (c.moveToNext());
    }
    return notes;
}

然后我在其他类中创建数组:

String[] title,created,content;

db = new DatabaseHelper(getActivity());

List<NotesFragmentCreate> allNotes = db.getAllNotes();
for (NotesFragmentCreate note : allNotes) {
     //how can I get each of record to store in this array?
     title[?] = note.getTitle(); 
     created[?] = note.getCreated();
     content[?] = note.getContent();
}

因为我想在listView

中强制执行这些数组
adapter = new ListNotesAdapter(getActivity(), title, created);

1 个答案:

答案 0 :(得分:1)

它的内部循环变量很简单,如下所示。

替换

String[] title,created,content;


List<NotesFragmentCreate> allNotes = db.getAllNotes();
for (NotesFragmentCreate note : allNotes) {
     //how can I get each of record to store in this array?
     title[?] = note.getTitle(); 
     created[?] = note.getCreated();
     content[?] = note.getContent();
}

List<NotesFragmentCreate> allNotes = db.getAllNotes();

    String[] title=new String[allNotes.size()];
    String[]  created=new String[allNotes.size()];
    String[] content=new String[allNotes.size()];
    int i=0;
    for (NotesFragmentCreate note : allNotes) {
         //how can I get each of record to store in this array?
         title[i] = note.getTitle(); 
         created[i] = note.getCreated();
         content[i] = note.getContent();
         i++
    }