1

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>
John
  • 5,166
  • 27
  • 31

2 Answers2

1

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.

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • I'm aware of that view, and it's close, but not quite what I need. "A view that shows items in a vertically scrolling list" I need a horizontally scrolling list (Why there is no HorizonatalListView, I'm not sure I understand...). I would love to use a ListView not least of which is for the benefits of not loading all the data at once, but I'd also prefer to not have to implement my own class when their has to be a reasonable way to do this (right?) – John Oct 06 '11 at 08:34
  • It's definetly the right path except the link goes to dev-smart which is dead. And the askers reccomendation is to just reimplement the whole thing from an AdapterView. So I guess like most things in the android UI it's crap and the right answer is reimplement the entire thing from the base classes =/ Android UI is frustrating me to no end lol – John Oct 06 '11 at 16:21
  • 1
    nm dev-smart is back up. As a secondary link for future people the class is available as part of https://github.com/dinocore1/DevsmartLib-Android – John Oct 06 '11 at 19:59
  • One final warning. This widget DOES NOT respect the wrap_content layout param. If you don't hardcode it's height it will make you spend the rest of the day trying to figure out why none of your views below it are showing up as it gobbles all of the screen space. This is a pretty badly implemented widget, but it's the only open source implementation. – John Oct 07 '11 at 06:04
0

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

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • I'm not sure I understand. I understand the inflator (this is what ends up being called when you call SimpleCursorAdapter.newView. And a ViewBinder I also get from the SimpleCursorAdapter, it simply lets tell bindView how to bind data to your views. I guess I'm just completely missing how I would bridge this gap – John Oct 06 '11 at 19:26
  • if you are not using a listView, you can use a ViewBinder to bind a series of views to a series of datas. – njzk2 Oct 07 '11 at 09:11