How can I go about extending SimpleCursorAdapter to allow for the following:
2 fragments, one menu one detail. Menu ListFragment is a list of tables, detail ListFragment shows the result of a query against these tables. The detail ListFragment gets passed a table name from the selection in the menu ListFragment. In onActivityCreated inside the detail ListFragment, all records are selected into a cursor. This cursor is passed into a SimpleCursorAdapter. This SimpleCursorAdapter is then set as the ListAdapter for the detail ListFragment.
What I can't figure out is how to dynamically change the SimpleCursorAdapter to display the correct number of columns based on the cursor results. I have the column names from Cursor.getColumnNames(), and I can throw these into the String[] from parameter on the SimpleCursorAdapter constructor. But how do I dynamically create the views necessary for the int to parameter? Will SimpleCursorAdapter just not work for this situation as it i开发者_StackOverflows looking for ids built off an xml layout file? Should I move on to using a LoaderManager with a CursorLoader? Will that be a more flexible solution?
You should move onto using LoaderManager with a CursorLoader.
As SimpleCursorAdapter says in part:
This constructor is deprecated. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors.
Using a LoaderManager/CursorLoader won't solve your issue with populating SimpleCursorAdapter. But you should certainly use it in order to populate your list off of the UI thread and handle configuration changes of your Activities efficiently.
Here's how to map Cursor column names to TextViews for each row:
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);
This will map the 3 columns in your cursor to 3 TextViews in your layout file
So your res/layout/custom_row.xml can look like this:
<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>
In the real world.. you may find better results with a TableLayout.
For CursorLoader, take a look at http://developer.android.com/guide/components/loaders.html They provide an excellent example of using a CursorLoader and LoaderManager for CursorAdapters which is what you need to do.
Hope that helps!
精彩评论