2011-10-04 43 views
0

In the Notepad example,它們顯示標題列表。你將如何改變它,以更小的字體顯示正文文本的第二行?使用SimpleCursorAdapter顯示額外文本

下面是相關代碼:

private void fillData() { 
    // Get all of the rows from the database and create the item list 
    Cursor notesCursor = mDbHelper.fetchAllNotes(); 
    startManagingCursor(notesCursor); 

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

    // 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); 
} 

這是notes_row.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="10dp" 
android:minWidth="100dp"/> 

的意見明確規定,只顯示標題。我希望它給了我一些關於如何顯示正文文本的提示。 (我們不討論這是否是一個好主意,讓我們姑且認爲這需要做的,因爲我想在記事本例適應自己的應用程序。)

回答

0

只是另一個的TextView添加到您的行:

<?xml version="1.0" encoding="utf-8"?> 
<TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="10dp" 
android:minWidth="100dp"/> 
<TextView android:id="@+id/body" xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:minWidth="100dp" 
    android:fontSize="20dp"/> 

,改變你的方法如下:

private void fillData() { 
    // Get all of the rows from the database and create the item list 
    Cursor notesCursor = mDbHelper.fetchAllNotes(); 
    startManagingCursor(notesCursor); 

    // Create an array to specify the fields we want to display in the list (only TITLE) 
    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.body}; 

    // 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); 
} 

請注意,它可能不是NotesDbAdapter.KEY_BODY檢索身柱,你應該能夠在你的NotesDbAdapter類看,在恆定它引用了正文的名稱並使用它。

此外 - 玩字體大小一些(android:fontSize =)... 20dp可能會非常小,你甚至不能看到它(我還沒有測試過)。