Android LinearLayout从数据库填充子级
我有一个 HorizontalScollView。它有一个 LinearLayout 子级。由于我无法将 SimpleCursorAdapter 绑定到它(显然),如何使用基于数据库内容创建的子项填充线性布局?
我已经尝试过:
SimpleCursorAdapter summaryItems = new SimpleCursorAdapter(this, R.layout.summary_item, summaryItemsCursor, from, to);
int count = summaryItems.getCount();
View new_view;
for (int i = 0; i < count; i++) {
new_view = (summaryItems.newView(this, summaryItemsCursor, mNewsContainer));
mNewsContainer.addView(new_view);
}
我希望 SimpleCursorAdapter.newView() 返回的视图可用,但显然不行。我对 Android 很陌生,完全不知道执行此操作的正确方法。
XML供参考:
<HorizontalScrollView android:id="@+id/news_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/news_container"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</LinearLayout>
</HorizontalScrollView>
和summary_item
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/news_gallery_item"
android:layout_marginRight="@dimen/news_gallery_item_spacing"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="@+id/news_gallery_item_image"
android:adjustViewBounds="true"
android:maxHeight="@dimen/news_gallery_image_height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/news_gallery_item_tagline"
style="@style/news_gallery_text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
I have a HorizontalScollView. It has a LinearLayout child. How do I populate the linear layout with children created based on content from a database since I can't bind a SimpleCursorAdapter to it (obviously)?
I've tried:
SimpleCursorAdapter summaryItems = new SimpleCursorAdapter(this, R.layout.summary_item, summaryItemsCursor, from, to);
int count = summaryItems.getCount();
View new_view;
for (int i = 0; i < count; i++) {
new_view = (summaryItems.newView(this, summaryItemsCursor, mNewsContainer));
mNewsContainer.addView(new_view);
}
I was hoping that the view returned by the SimpleCursorAdapter.newView() would be usable, but apparently not. I'm quite new to android and completely lost as to the right way to do this.
XML's for reference:
<HorizontalScrollView android:id="@+id/news_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/news_container"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</LinearLayout>
</HorizontalScrollView>
and summary_item
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/news_gallery_item"
android:layout_marginRight="@dimen/news_gallery_item_spacing"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="@+id/news_gallery_item_image"
android:adjustViewBounds="true"
android:maxHeight="@dimen/news_gallery_image_height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/news_gallery_item_tagline"
style="@style/news_gallery_text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来(并且以某种方式看起来)就像您想要使用
列表视图
。可以在此处找到教程。要填充
ListView
,您可以使用SimpleCursorAdapter
。由于您需要水平显示,因此可以使用
HorizonatalListView
-class,这是用户创建的普通ListView
扩展(因此您可以使用它的所有方法)。相应的问题可以在此处找到。This sounds (and somehow looks) like you'll want to use a
ListView
. A tutorial might be found here.To populate the
ListView
, you can then use aSimpleCursorAdapter
.Since you need it horizontally, you can use the
HorizonatalListView
-class, which is a user-created extension to a normalListView
(so you can use all it's methods). The corresponding question can be found here.项目视图上带有 Inflater 的 ViewBinder 应该可以解决问题
A ViewBinder with an Inflater on your item view should do the trick