使用LoaderManager/CursorLoader無法解決您在填充SimpleCursorAdapter時遇到的問題。但是您應該使用它來將您的列表填充到UI線程中,並有效地處理活動的配置更改。
這裏是如何光標列名TextViews映射爲每一行:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
R.layout.custom_row,
null,
new String[] { "columnName_1", "columnName_2", "columnName_3" },
new int[] { R.id.txtCol1, R.id.txtCol2, R.id.txtCol3 }, 0);
setListAdapter(adapter);
這會在你的光標3列3個TextViews映射在你的佈局文件
所以,你的RES /佈局/ custom_row.xml可以是這樣的:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/txtCol1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your column 1 text will end up here!" />
<TextView android:id="@+id/txtCol2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your column 2 text will end up here!" />
<TextView android:id="@+id/txtCol3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your column 3 text will end up here!" />
</LinearLayout>
在現實世界中..你可以找到一個TableLayout更好的結果。
對於CursorLoader,看看http://developer.android.com/guide/components/loaders.html 他們提供一種使用CursorAdapters一個CursorLoader和LoaderManager這是你需要做的一個很好的例子。
希望有幫助!
但他們有第二個構造函數,現在不被棄用,是否正確? – theblang 2013-11-20 15:54:16
@mattblang。什麼不被棄用? – 2014-01-02 19:51:21
@IgorGanapolsky:有[帶有標誌參數的構造函數](http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#SimpleCursorAdapter%28android.content.Context,%20int,%20android.database.Cursor ,%20java.lang.String [],%20int [],%20int%29),它只表示「標準構造函數」,並且沒有棄用信息。 – akavel 2014-01-08 23:39:52