0

I want to know how to set ListView's height through calculating the item view, someone says in this way.

ListAdapter listAdapter = listView.getAdapter();

if (listAdapter == null) {
    return;
}

int totalHeight = 0;

for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);
    listItem.measure(0, 0);
    totalHeight += listItem.getMeasuredHeight();
}

But what if I put into the ListView for a class based on BaseAdapter?

tomrozb
  • 25,773
  • 31
  • 101
  • 122
  • 2
    is the data which you want to put it in the listview static ? or dynamic ? – William Kinaan Mar 10 '13 at 14:41
  • dynamic! then i will use the "adapter.notifyDataSetChanged();"to update the adapter – user1659372 Mar 10 '13 at 15:02
  • 2
    so in case of `dynamic`, are you using specific structure in side your adapter to save the data, something like `hashmap` , `arraylist` or you make your adapter read the data from external source such as `server connection` or `database`? – William Kinaan Mar 10 '13 at 15:09

1 Answers1

0

BaseAdapter only implement ListAdapter adapter interface, so there is no reason why your code shouldn't work with BaseAdapter. You can try this:

BaseAdapter listAdapter = (BaseAdapter)listView.getAdapter();

if (listAdapter == null) {
    return;
}

int totalHeight = 0;

for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);
    listItem.measure(0, 0);
    totalHeight += listItem.getMeasuredHeight();
}
gingo
  • 3,149
  • 1
  • 23
  • 32