2011-11-28 72 views
0

我正在從android網站的記事本教程工作。我創建了一個包含2個條目的數據庫:名稱和地址。當應用程序運行時,只顯示名稱。我想顯示姓名和地址。我編輯了我的filldata方法,但它不起作用。看到我的代碼如下。有什麼建議麼?SQLite更改列表的佈局

private void fillData() { 
    Cursor notesCursor = mDbHelper.fetchAllNotes(); 
    startManagingCursor(notesCursor); 

    // Create an array to specify the fields we want to display in the list (TITLE + BODY) 
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY}; 

    // and an array of the fields we want to bind those fields to (in this case just text1) 
    int[] to = new int[]{R.id.text1}; 

    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter notes = 
     new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to); 
    setListAdapter(notes); 
} 

回答

2

在你R.layout.notes_row文件,你應該有兩個TextViews一個標題和一個地址,然後你會寫:

String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY}; 

    // and an array of the fields we want to bind those fields to (in this case just text1) 
    int[] to = new int[]{R.id.text1, R.id.id_of_the_second_text}; 

    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter notes = 
     new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to); 
    setListAdapter(notes); 

編輯: R.layout.notes_row可能是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
<TextView android:id="@+id/text1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 
<TextView android:id="@+id/text2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 
</LinearLayout> 
+0

謝謝。我試圖在我的notes_row文件中創建第二個textview,並繼續收到錯誤。我的代碼如下所示。任何想法?'$ <?xml version =「1.0」encoding =「utf-8」?> DMC

+0

@ user876343請參閱我編輯的答案。在xml佈局中,你必須只有一個根元素,因此你必須用另一個元素(我使用'LinearLayout',但還有其他選項)包裝你的兩個'TextView'。用我的上面的代碼,你將有一個佈局,兩個'TextView',一個在另一個之上。 – Luksprog

+0

謝謝,我自己在那裏工作!歡呼的幫助! – DMC