GAMA
GAMA

Reputation: 5996

ListView with ArrayAdapter showing only 1 item for height wrap_content

I'm having a issue where my ListView is displaying only one row even though ArrayList containing data has 2 items in it.

For clarification, YES, I'm using ListView inside a ScrollView but to balance it out I'm using custom ExpandableHeightListView (retrieved from one of the answers on SO) which is useful for exactly this purpose. This ListView is not scrollable. It has fixed height.

Also, within the same ScrollView, couple of other ListView are working fine but this one ListView is not working properly.

Going further, when I logged the position variable value inside getView() method, it's always 0 for this ListView while for other ListView, it's increasing sequentially.

I've compared both Adapter's code and they are identical but still current ListView is not working.

I tried with overriding getView() method, but to no avail.

Any help appreciated.

EDIT

If I give fixed height to ListView like 250dp, both the rows are getting displayed but it shows only 1 row with height set to wrap_content

Upvotes: 0

Views: 5369

Answers (3)

Munier Parker
Munier Parker

Reputation: 341

This solution worked for me and also adjusts the layout automatically. On the top-level ListView adapter, inside the getView() method, you will create a view that references a layout with another ListView. Assuming that "inner" ListView is declared as "listView" with its adapter called "adapter", try using the code below. Also, ensure that the inner ListView layout is set to WRAP_CONTENT, so that even if the height is correct, it is not being overridden by the layout constraints.

listView.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            listView.getLayoutParams().height = adapter.getCount() * {whatever your list item height is}  OR do something else to add up all of the heights;
            listView.requestLayout();
            listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    }
);

Upvotes: 0

Treeline
Treeline

Reputation: 485

I know this is not what you want to hear, but seriously, whatever you are programming, consider displaying it on tabs or similar, such that you don't need the scrollview.

If you really must have so much data on one screen, consider using something like a LinearLayout with an adapter to display your data.

Listviews are not designed to work inside a scrollview because they are scrollable themselves. You can "hack" your way around it, but it's not nice. If you really need the features that listviews provide, consider splitting up your screens into tabs, displaying a listview or two in each tab.

Upvotes: 1

Avishek Das
Avishek Das

Reputation: 699

As I know that ListView within a scrollView does not worked properly.

Because both widget has scroll so they does not worked well.

So I recommend to delete the ScrollView

Thanks..

Upvotes: 1

Related Questions