Example usage for android.widget ListAdapter getCount

List of usage examples for android.widget ListAdapter getCount

Introduction

In this page you can find the example usage for android.widget ListAdapter getCount.

Prototype

int getCount();

Source Link

Document

How many items are in the data set represented by this Adapter.

Usage

From source file:Main.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    if (listView == null)
        return;//from   w  ww. j a v  a 2 s  .  c  o m
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        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();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:Main.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition  
        return;/*from  ww  w  .j a v a2 s.  com*/
    }

    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();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:Main.java

/**
 * get AbsListView height according to every children
 * /*w  w  w .  j  a  va  2 s.c o m*/
 * @param view
 * @return
 */
public static int getAbsListViewHeightBasedOnChildren(AbsListView view) {
    ListAdapter adapter;
    if (view == null || (adapter = view.getAdapter()) == null) {
        return 0;
    }

    int height = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
        View item = adapter.getView(i, null, view);
        if (item instanceof ViewGroup) {
            item.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }
        item.measure(0, 0);
        height += item.getMeasuredHeight();
    }
    height += view.getPaddingTop() + view.getPaddingBottom();
    return height;
}

From source file:Main.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;//from   w w w. j a v a  2  s  . c  om
    }

    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();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    if (listView.isInLayout() == false) {
        listView.requestLayout();
    }
}

From source file:Main.java

public static int getAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace) {
    int totalHeight = 0;
    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    absListView.measure(w, h);//from   w w  w  . j  ava  2s.c o  m
    ListAdapter mListAdapter = absListView.getAdapter();
    if (mListAdapter == null) {
        return totalHeight;
    }

    int count = mListAdapter.getCount();
    if (absListView instanceof ListView) {
        for (int i = 0; i < count; i++) {
            View listItem = mListAdapter.getView(i, null, absListView);
            listItem.measure(w, h);
            totalHeight += listItem.getMeasuredHeight();
        }
        if (count == 0) {
            totalHeight = verticalSpace;
        } else {
            totalHeight = totalHeight + (((ListView) absListView).getDividerHeight() * (count - 1));
        }

    } else if (absListView instanceof GridView) {
        int remain = count % lineNumber;
        if (remain > 0) {
            remain = 1;
        }
        if (mListAdapter.getCount() == 0) {
            totalHeight = verticalSpace;
        } else {
            View listItem = mListAdapter.getView(0, null, absListView);
            listItem.measure(w, h);
            int line = count / lineNumber + remain;
            totalHeight = line * listItem.getMeasuredHeight() + (line - 1) * verticalSpace;
        }

    }
    return totalHeight;

}

From source file:Main.java

/**
 * Set the ListView height based on the number of rows and their height
 * <p/>//  w w  w. j a va2 s  .co  m
 * Height for ListView needs to be set if used as a child of another ListView.
 * The child ListView will not display any scrollbars so the height needs to be
 * set so that all the rows are visible
 *
 * @param listView    the list view
 * @param extraHeight extra bottom padding
 */
@SuppressWarnings("SameParameterValue")
public static void setListViewHeightBasedOnChildren(ListView listView, int extraHeight) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        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();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = extraHeight + totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:Main.java

public static int getAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace) {
    int totalHeight = 0;
    int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    absListView.measure(w, h);/* w  w  w. j  a va  2  s.c o m*/
    ListAdapter mListAdapter = absListView.getAdapter();
    if (mListAdapter == null) {
        return totalHeight;
    }

    int count = mListAdapter.getCount();
    if (absListView instanceof ListView) {
        for (int i = 0; i < count; i++) {
            View listItem = mListAdapter.getView(i, null, absListView);
            listItem.measure(w, h);
            totalHeight += listItem.getMeasuredHeight();
        }
        if (count == 0) {
            totalHeight = verticalSpace;
        } else {
            totalHeight = totalHeight + (((ListView) absListView).getDividerHeight() * (count - 1));
        }

    } else if (absListView instanceof GridView) {
        int remain = count % lineNumber;
        if (remain > 0) {
            remain = 1;
        }
        if (mListAdapter.getCount() == 0) {
            totalHeight = verticalSpace;
        } else {
            View listItem = mListAdapter.getView(0, null, absListView);
            listItem.measure(w, h);
            int line = count / lineNumber + remain;
            totalHeight = line * listItem.getMeasuredHeight() + (line - 1) * verticalSpace;
        }

    }
    return totalHeight;

}

From source file:Main.java

public static void setAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace) {

    if (lineNumber == 0) {
        return;//from  w w  w .jav a  2s .  c  o  m
    }
    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    ListAdapter mListAdapter = absListView.getAdapter();
    if (mListAdapter == null) {
        return;
    }
    int totalHeight = 0;
    int count = mListAdapter.getCount();
    ViewGroup.LayoutParams params = absListView.getLayoutParams();
    if (absListView instanceof ListView) {
        for (int i = 0; i < count; i++) {
            View listItem = mListAdapter.getView(i, null, absListView);
            listItem.measure(w, h);
            totalHeight += listItem.getMeasuredHeight();
        }
        if (count == 0) {
            params.height = verticalSpace;
        } else {
            params.height = totalHeight + (((ListView) absListView).getDividerHeight() * (count - 1));
        }

    } else if (absListView instanceof GridView) {
        int remain = count % lineNumber;
        if (remain > 0) {
            remain = 1;
        }
        if (mListAdapter.getCount() == 0) {
            params.height = verticalSpace;
        } else {
            View listItem = mListAdapter.getView(0, null, absListView);
            listItem.measure(w, h);
            int line = count / lineNumber + remain;
            params.height = line * listItem.getMeasuredHeight() + (line + 1) * verticalSpace;
        }

    }

    ((MarginLayoutParams) params).setMargins(0, 0, 0, 0);
    absListView.setLayoutParams(params);

}

From source file:Main.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;//from   www  .j av  a2s .  co m
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:Main.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;//from ww  w.j a  v a 2  s. c  o m
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
        }
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}