【问题标题】:Call all records into an array of String将所有记录调用到 String 数组中
【发布时间】:2015-09-12 10:46:03
【问题描述】:

我需要从 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);

【问题讨论】:

    标签: android arrays sqlite


    【解决方案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++
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-12
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 2016-05-04
      • 1970-01-01
      相关资源
      最近更新 更多