Android LinearLayout从数据库填充子级

发布于 2024-12-08 08:23:36 字数 1847 浏览 2 评论 0原文

我有一个 Horizo​​ntalScollView。它有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-12-15 08:23:37

这听起来(并且以某种方式看起来)就像您想要使用 列表视图。可以在此处找到教程。

要填充 ListView,您可以使用 SimpleCursorAdapter


由于您需要水平显示,因此可以使用 Horizo​​natalListView-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 a SimpleCursorAdapter.


Since you need it horizontally, you can use the HorizonatalListView-class, which is a user-created extension to a normal ListView (so you can use all it's methods). The corresponding question can be found here.

预谋 2024-12-15 08:23:37

项目视图上带有 Inflater 的 ViewBinder 应该可以解决问题

A ViewBinder with an Inflater on your item view should do the trick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文