Android SimpleCursorAdapter与LoaderManager/CursorLoader
如何扩展SimpleCursorAdapter以允许以下内容: 2个片段,一个菜单,一个细节。菜单ListFragment是一个表列表,详细信息ListFragment显示对这些表的查询结果。明细ListFragment从菜单ListFragment中的选择中获得一个表名。在详细信息列表片段内创建的onActivityCreated中,所有记录都被选择到一个游标中。此光标被传递到SimpleCorsorAdapter中。然后将此SimpleCursorAdapter设置为详细信息ListFragment的ListAdapterAndroid SimpleCursorAdapter与LoaderManager/CursorLoader,android,android-loadermanager,Android,Android Loadermanager,如何扩展SimpleCursorAdapter以允许以下内容: 2个片段,一个菜单,一个细节。菜单ListFragment是一个表列表,详细信息ListFragment显示对这些表的查询结果。明细ListFragment从菜单ListFragment中的选择中获得一个表名。在详细信息列表片段内创建的onActivityCreated中,所有记录都被选择到一个游标中。此光标被传递到SimpleCorsorAdapter中。然后将此SimpleCursorAdapter设置为详细信息ListFrag
我搞不懂的是如何根据光标结果动态更改SimpleCorsorAdapter以显示正确的列数。我有Cursor.getColumnNames()中的列名,我可以将它们放入SimpleCursorAdapter构造函数上的字符串[]from参数中。但是如何动态创建int-to参数所需的视图呢?SimpleCursorAdapter在寻找基于xml布局文件构建的ID时,是否不适合这种情况?我应该继续使用带游标装载器的LoaderManager吗?这是一个更灵活的解决方案吗?您应该将LoaderManager与CursorLoader结合使用 如部分所述:
此构造函数已弃用。不鼓励使用此选项,因为它会导致在应用程序的UI线程上执行光标查询,从而导致响应性差,甚至应用程序没有响应错误 使用LoaderManager/CursorLoader无法解决填充SimpleCursorAdapter的问题。但您当然应该使用它来填充UI线程的列表,并有效地处理活动的配置更改 以下是如何将光标列名映射到每行的文本视图:
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个文本视图
因此,您的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>
在现实世界中。。使用表格布局可能会获得更好的结果
对于游标装入器,请查看
它们提供了一个很好的例子,说明了如何为游标适配器使用游标装入器和装入器管理器,这正是您需要做的
希望有帮助 但是他们现在有了第二个构造器,这不是不推荐的,对吗?@mattblang。什么不是不推荐的?@IgorGanapolsky:有,上面说的只是“标准构造函数”,没有不推荐的信息。为什么有人会使用TableLayout?网格布局是未来的潮流。