1

以下を可能にするために SimpleCursorAdapter を拡張するにはどうすればよいですか。

2 つのフラグメント、1 つのメニュー 1 つの詳細。メニュー ListFragment はテーブルのリストで、詳細 ListFragment はこれらのテーブルに対するクエリの結果を示します。詳細 ListFragment には、メニュー ListFragment の選択からテーブル名が渡されます。詳細 ListFragment 内の onActivityCreated では、すべてのレコードが選択されてカーソルになります。このカーソルは SimpleCursorAdapter に渡されます。この SimpleCursorAdapter は、詳細 ListFragment の ListAdapter として設定されます。

私が理解できないのは、SimpleCursorAdapter を動的に変更して、カーソルの結果に基づいて正しい列数を表示する方法です。Cursor.getColumnNames() からの列名があり、これらを SimpleCursorAdapter コンストラクターのパラメーターから String[] にスローできます。しかし、int to パラメータに必要なビューを動的に作成するにはどうすればよいでしょうか? SimpleCursorAdapter は、xml レイアウト ファイルから構築された ID を探しているため、この状況では機能しませんか? CursorLoader で LoaderManager を使用する必要がありますか? それはより柔軟なソリューションになるでしょうか?

4

2 に答える 2

3

CursorLoaderでLoaderManagerを使用することに移る必要があります。

SimpleCursorAdapterが部分的に言うように:

このコンストラクタは非推奨です。このオプションは、アプリケーションのUIスレッドでカーソルクエリが実行される結果となり、応答性が低下したり、アプリケーションが応答しないエラーが発生したりする可能性があるため、お勧めしません。

于 2011-11-01T23:28:52.100 に答える
0

LoaderManager/CursorLoader を使用しても、SimpleCursorAdapter の設定に関する問題は解決されません。ただし、UI スレッドからリストを作成し、Activity の構成変更を効率的に処理するには、必ずこれを使用する必要があります。

Cursor 列名を各行の 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 つの TextView にマップされます。

したがって、 res/layout/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 を使用する優れた例を提供します。

それが役立つことを願っています!

于 2012-06-30T04:32:15.007 に答える