0

我正在尝试使用关系数据库中的数据填充 ListView。我想要完成的是在 NURSE_TABLE 中显示每位护士,以及分配给该护士的房间号。结果我得到的是显示两个按钮的布局和一个空的 ListView。

创建数据库的代码是:

public void onCreate(SQLiteDatabase db) {
// create NURSE_TABLE
db.execSQL("CREATE TABLE " + NURSE_TABLE + " (" + KEY_NURSE_ROWID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " +          KEY_LNAME
        + " TEXT NOT NULL, " + KEY_FNAME + " TEXT NOT NULL, "
        + KEY_MI + " TEXT, " + KEY_EMAIL + " TEXT NOT NULL, "
        + KEY_PHONE_PRIMARY + " TEXT NOT NULL, "
        + KEY_PHONE_SECONDARY + " TEXT, " + KEY_FTE
        + " TEXT NOT NULL, " + KEY_DUTY_STATUS + " TEXT NOT NULL);");

// create ROOM_TABLE
db.execSQL("CREATE TABLE " + ROOM_TABLE + " (" + KEY_ROOM_ROWID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TYPE_PATIENT
        + " TEXT, " + KEY_OCCUPIED + " TEXT NOT NULL, "
        + KEY_ROOM_NUMBER + " TEXT NOT NULL, "
        + KEY_ASSIGNED_NURSE + " INTEGER, " 
        + " FOREIGN KEY (" + KEY_ASSIGNED_NURSE + ") REFERENCES " 
        + NURSE_TABLE + " (" + KEY_NURSE_ROWID + "));");
    } // onCreate

活动代码如下:

public void displayNurseRoster(){

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

    // int variables filled with NURSE_TABLE data
    int[] to_nurseTable = new int[] {

            // int list for data from NURSE_TABLE fields
            R.id.rosterDutyStatus,
            R.id.rosterListLname, 
            R.id.rosterListFname, 
            R.id.rosterListMI, 
            R.id.rosterListID,

            // int list for data from ROOM_ROWID fields
            R.id.firstAssignment,
            R.id.secondAssignment,
            R.id.thirdAssignment,
            R.id.fourthAssignment,
            R.id.fifthAssignment,
            R.id.sixthAssignment };

    // String array holding data fields from NURSE_TABLE 
    String[] from_nurseTable = new String [] {

            // fields from NURSE_TABLE
            DbCommunicator.KEY_DUTY_STATUS,
            DbCommunicator.KEY_LNAME, 
            DbCommunicator.KEY_FNAME, 
            DbCommunicator.KEY_MI,
            DbCommunicator.KEY_NURSE_ROWID,
            DbCommunicator.KEY_ROOM_NUMBER}; 

    // instantiate instance of DbCommunicator object
    rosterView = new DbCommunicator(this);

    // open instance
    rosterView.open();

    // get & manage cursor for NURSE_TABLE data
    Cursor nurseTableCursor = rosterView.getNurseRosterCursor(this);
    startManagingCursor(nurseTableCursor);

    // instantiate cursor adaptor
    ListAdapter nurseTableAdapter = new SimpleCursorAdapter(this,
    R.layout.nurse_list, nurseTableCursor, from_nurseTable, to_nurseTable);

    nurseTableCursor.moveToNext();

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

生成光标的代码如下所示:

public Cursor getNurseRosterCursor (Context context) {

    SQLiteQueryBuilder nurseQueryBuilder = new SQLiteQueryBuilder();
    nurseQueryBuilder.setTables("NURSE_TABLE, ROOM_TABLE"); 

    String[] returnedColumns = new String[] {
            "Nurse_Table._id", 
            "Nurse_Table._dutyStatus",
            "Nurse_Table._lName",
            "Nurse_Table._FName",
            "Nurse_Table._mi",
            "Room_Table._id",
            "Room_Table._roomNumber"};

    Cursor nurseCursor = nurseQueryBuilder.query(sqldb, returnedColumns,
            null, null, null, null, null);

    return nurseCursor;
}

最后我的布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#81BEF7"
android:orientation="vertical" >

<Button
    android:id="@+id/btnAddNurseToRoster"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/add" 
    android:focusable="true"/>

<Button
    android:id="@+id/btnRemoveNurseFromRoster"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/btnAddNurseToRoster"
    android:layout_toRightOf="@+id/btnAddNurseToRoster"
    android:text="@string/delete" 
    android:focusable="true"/>

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/btnAddNurseToRoster"
    android:textColor="#FFFFFF"
    android:textStyle="bold"/>

</RelativeLayout>

当我最初开始时,我只是用 Nurses Table 中的数据填充 ListView,效果很好,但是自从我开始尝试让这两个表一起工作以来,我度过了一段美好的时光。

任何帮助/方向将不胜感激。

4

1 回答 1

0

您通话中的两个表setTables()未正确连接。

和数组from_nurseTableto_nurseTable匹配。

您没有提到要在列表视图中显示的内容,但我猜它可能类似于此查询的结果:

SELECT nurse_table._id,_dutyStatus,_lName,_FName,_mi,group_concat(_roomNumber)
  FROM nurse_table LEFT JOIN room_table ON nurse_table._id = _assignedNurse
  GROUP BY nurse_table._id
  ORDER BY _lName,_FName /* or whatever */

当您不知道如何构建 SQL 查询时,在桌面上的数据库副本中测试查询通常更有帮助。(有许多 SQLite 管理工具,或者作为最后的恢复使用 sqlite(.exe) 命令行工具。)

于 2012-09-09T14:41:52.153 回答