0

I have a form with a number of EditView fields in it. The data for these fields are loaded from a database (in the onCreate() method). The last object on the screen should be a ListView that should show all related data records to the record being show. All the data is correctly loading, and the adapter for this seems to work ok. It loads the correct data, it formats the data correctly into the two-line layout specified by the .xml used by the adapter.

The problem is that the ListView on the screen is "shrunk" to show only one item, and adds a scrollbar if there's more items. I expected the ListView to expand in size to show all records, and the screen itself being scrollable (everything is wrapped inside a ScrollView).

So, the XML looks like this:

<ScrollView
    android:layout_height="wrap_content"
    android:id="@+id/ScrollView1">
  <RelativeLayout
      android:layout_height="wrap_content"
      android:id="@+id/RelativeLayout1">
    <EditView
      android:id="@+id/EditView1>
    </EditView>
    <ListView
      android:id="@+id/ListView1
      android:layout_height="wrap_content"
      android:layout_below="@id/EditView1>
      android:divider="#b5b5b5"
      android:dividerHeight="1dp" />

I then use a custom BaseAdapter to fill data into the ListView

listView = (ListView) findViewById(R.id.ListView1);
dbRecords = db.getAllRecordsByRecordId(recordId);
CBA_Records adapter = new CBA_Records(this, dbRecords);
listView.setAdapter(adapter);

This is all the same stuff that I've done before, except this is all wrapped inside the scrollview. The reason for this is that there might be more fields than will fit on a smaller screen (or horizontal screen), so the screen must be scrollable. And, the listview must also be there ...

Any suggestions?

Lars
  • 15
  • 1
  • 4

2 Answers2

2

A little more onto what invertigo said. ListView inside a ScrollView is not recommended.

Make your ListView the root, and set it's width and height to "match_parent". Put the other stuff that's above the ListView (the header) in a separate xml. Then inflate the new xml file for the header use the addHeaderView() method to add it as a header to the ListView (it looks like you want everything to scroll).

Wenger
  • 989
  • 2
  • 12
  • 35
  • Thanks guys. I knew having the ListView inside the ScrollView wasn't recommended, but I couldn't figure out any other ways of doing what I wanted to do. After checking out addHeaderView(), I adapted the solution found at SamColes.co.uk, and after some debugging, I got that working. Now, all of it scrolls as it should :) – Lars May 28 '13 at 20:50
  • url : http://www.samcoles.co.uk/mobile/android-listactivity-with-a-header-or-footer/ – Lars May 28 '13 at 20:52
0

ListView is vertically scrolling automatically, so you now have two vertical scroll areas, which do you expect to consume the scroll event? Either set your ListView to a static height (not recommended), or design your layout so only the area you want to scroll (ie the ListView) has scrolling capabilities and remove the ScrollView. Also set your base layout, in this case the RelativeLayout, to height=match-parent, and ListView height=0dp weight=1 to prevent the ListView from being "shrunk".

Alternatively, take a look at this solution if you dont want the ListView scrolling independantly: android listview display all available items without scroll with static header

Community
  • 1
  • 1
invertigo
  • 6,336
  • 5
  • 39
  • 64