【问题标题】:Populate ListView with data from SQLite database使用 SQLite 数据库中的数据填充 ListView
【发布时间】:2012-08-31 04:32:33
【问题描述】:

我一直在努力尝试使用 SQLite 数据库中的数据填充 ListView,但似乎无法弄清楚。我在这里阅读了无数的教程和同样无数的帖子,但我显然没有得到关键的东西。希望有人能给我一个提示,说明为什么以下两段代码不能一起工作,或者我是否应该完全看其他东西。任何帮助,将不胜感激。我得到的结果是强制关闭。

启动填充 ListView 对象的方法

public void displayNurseRoster(){

    listView = (ListView) findViewById(R.id.list);

    // create instance of DbCommunicator
    DbCommunicator rosterView = new DbCommunicator(this);

    // open instance
    rosterView.open();

    // instantiate SimpleCursorAdapter instance and set value
    SimpleCursorAdapter cursorAdapter;
    cursorAdapter = rosterView.getRosterListViewAdapter(this);

    // close database instance
    rosterView.close();

    // set adapter to listView
    listView.setAdapter(cursorAdapter);
}

返回 SimpleCursorAdapter 的方法:

public SimpleCursorAdapter getRosterListViewAdapter (Context context) {

    // method variables 
    int[] to = new int[] {R.id.rosterListLname};
    // used ArrayList because of unpredictability of array length
    List<String> dataArray = new ArrayList<String>(); 
    String[] columns = new String[] {KEY_NURSE_ROWID, KEY_LNAME}; 

    // create cursor
    Cursor cursor = sqldb.query(NURSE_TABLE, columns, null, null, null,
            null, KEY_LNAME);

    int iLname = cursor.getColumnIndex(KEY_LNAME);

    cursor.moveToFirst();

    String result = "";

    while(!cursor.isAfterLast()){
        result = result 
                + cursor.getString(iLname) + "\n";
        dataArray.add(result);

    }
    // convert ArrayList to String array for use with SimpleCursorAdapter
    String [] from = (String[]) dataArray.toArray();

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(context,
            R.layout.edit_roster, cursor, from, to);

    return adapter;
}

【问题讨论】:

  • 仍在解决这个小问题。任何帮助将不胜感激。

标签: sqlite listview simplecursoradapter


【解决方案1】:

好的,我开始使用它,并想分享以帮助其他在此问题上苦苦挣扎的人,以及 2) 获得有关我的修复的反馈,以防有更多经验的人提出更好的建议。

我实际上想在列表视图中显示三个项目,所以这是有效的代码。我遇到的一个问题是我正在扩展 ListActivity,但在另一篇 StackOverflow 帖子中发现,当只有一个列表视图需要担心时,这不是一个好主意。

package com.deadEddie.staffingmanagement;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class EditRoster extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_roster);
    displayNurseRoster();   
}

public void displayNurseRoster(){

    ListView listView = (ListView) findViewById(R.id.list);

    int[] to = new int[] {
            R.id.rosterListLname, 
            R.id.rosterListFname, 
            R.id.rosterListMI };

    String[] from = new String [] {
            DbCommunicator.KEY_LNAME, 
            DbCommunicator.KEY_FNAME, 
            DbCommunicator.KEY_MI};

    // create instance of DbCommunicator
    DbCommunicator rosterView = new DbCommunicator(this);

    // open instance
    rosterView.open();

    // get & manage cursor
    Cursor cursor = rosterView.getRosterCursor(this);
    startManagingCursor(cursor);

    // instantiate cursor adaptor
    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.nurse_list, cursor, from, to);
    cursor.moveToNext();

    // set adapter to listView
    listView.setAdapter(adapter);
}// displayNurseRoster()
}

这是我的 DbCommunicator 类中的方法。最终不同的是,我没有创建 SQLiteQueryBuilder 的实例并使用它,而是使用了 sqldb.query(...),如上所示。我不确定有什么区别,但它终于成功了。如果有人想分享,请做。

public Cursor getRosterCursor (Context context) {

    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(NURSE_TABLE);

    Cursor cursor = queryBuilder.query(sqldb, new String[] {
            KEY_NURSE_ROWID, 
            KEY_LNAME,
            KEY_FNAME,
            KEY_MI},
            null, null, null, null, null);

    return cursor;
}

为其他人提供的其他一些新手课程: 1. 始终使用光标中的“_id”字段。没有它,光标就无法工作。
2. 简单的列表视图不需要 while 或 for 循环。游标适配器处理这个问题。 3. 光标上不需要调用moveToFirst()方法。

希望对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多