Skip to content Skip to sidebar Skip to footer

Limit Number Of Rows Of Listview

I am developing an application that needs to pair with other devices through Bluetooth. I was having trouble in order to show the list of paired devices in correct manner. I had al

Solution 1:

Firstly some points about your code:

  • layout_weight is only meaningful if an object has no size in a certain dimension, that is you set layout_height or layout_width to 0, so this has no effect on your code.
  • Setting the height of a ListView to wrap_content is pretty meaningless, and even if it works it's bad practice. Use either 'fill_parent' or a definite height.
  • The button is hidden because, as per the point above, the ListViews you have created have no predefined size so take up as much space as they can, pushing the button off the screen.

So let's think about what you really have there - it's just a single list with a button at the bottom (yes you may have headers or multiple sources of data in there but we'll get onto that).

The following layout will give you a ListView at the top and a Button at the bottom. The ListView will take up any space not being used by the Button. Notice how the Button is defined before the ListView in the layout - this causes Android to calculate how much height the button takes up before considering the ListView. It then gives the ListView the rest of the height available.

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"
  ><Buttonandroid:id="@+id/btscan"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:text="@string/btscan"
  /><ListViewandroid:id="@+id/all_devices"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_above="@id/btscan"android:stackFromBottom="true"
  /></RelativeLayout>

So that's the layout. Now lets consider the actual content of your list: you have a header, followed by a list of paired devices, followed by another header and then a list of new devices.

You can create this using a single Adapter - Commonsware provides a very good implementation called MergeAdapter but you could code your own. MergeAdapter doesn't directly let you a view (e.g. for the headers) but thankfully Commonsware also provides the SackOfViewsAdapter which allows you to add views to it, and then you can add the SackOfViewsAdapter to the MergeAdapter.

Here is some pseudo-code which should accomplish what is outlined above.

// This is the adapter we will use for our list with ListView.setAdapterMergeAdapterlistAdapter=newMergeAdapter();

// The first header - you'll need to inflate the actual View (myHeaderView1) from some resource// using LayoutInflater
List<View> firstHeaderViews = newList<View();
firstHeaderViews.add(myHeaderView1);
SackOfViewsAdapterfirstHeaderAdapter=newSackOfViewsAdapter(firstHeaderViews)

// Second header
List<View> secondHeaderViews = newList<View();
secondHeaderViews.add(myHeaderView2);
SackOfViewsAdaptersecondHeaderAdapter=newSackOfViewsAdapter(secondHeaderViews);

// Now to add everything to the MergeAdapter// First goes the first header
listAdapter.addAdapter(firstHeaderAdapter);

// Now your first list
listAdapter.addAdapter(firstListAdapter);

// Now your second header
listAdapter.addAdapter(secondHeaderAdapter);

// Now your second list
listAdapter.addAdapter(secondListAdapter);

// Now set the adapter to the list
myList.setAdapter(listAdapter)

The layout produced should look something like this. Note I extended the list to show how it behaves with a list longer than the screen; the button still remains visible. The red box marks the bounds of the screen.

Solution 2:

You can limit the number of rows shown at the list view, but not sure if that will really help you in what you want to achieve, because you're hiding information from the user that might be important to him. You can limit the number of rows by limiting the number of items you pass to the listview adapter, or you can set the visibility to 'gone' in the getView method when the list view reaches a certain position (you can check the 'position' parameter of getView()).

However, I would suggest you use only one list view (add a separator for the 'new/other devices' title into the view for a list item, but hide it by default, and then, as already suggested by suri, use headers and footers for the listview (to place the scan button).

Post a Comment for "Limit Number Of Rows Of Listview"