使用SimpleCursorAdapter显示额外文本
问题描述:
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"/>
的意见明确规定,只显示标题。我希望它给了我一些关于如何显示正文文本的提示。 (我们不讨论这是否是一个好主意,让我们姑且认为这需要做的,因为我想在记事本例适应自己的应用程序。)
答
只是另一个的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可能会非常小,你甚至不能看到它(我还没有测试过)。