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 setGridViewSize(GridView grid) {
    ListAdapter myListAdapter = grid.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;//from   ww  w .  ja v  a 2s . c o m
    }

    //set listAdapter in loop for getting final size
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(grid.getWidth() / grid.getNumColumns(),
            View.MeasureSpec.AT_MOST);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < ((myListAdapter.getCount() / grid.getNumColumns())
            + myListAdapter.getCount() % grid.getNumColumns()); i++) {
        view = myListAdapter.getView(i, view, grid);
        if (i == 0) {
            view.setLayoutParams(new GridView.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        totalHeight += view.getMeasuredHeight();
    }

    //setting listview item in adapter
    ViewGroup.LayoutParams params = grid.getLayoutParams();
    params.height = totalHeight
            + (grid.getHorizontalSpacing() * (((myListAdapter.getCount() - 1) / grid.getNumColumns())
                    + (myListAdapter.getCount() % grid.getNumColumns())));
    grid.setLayoutParams(params);
}

From source file:com.optimusinfo.elasticpath.cortex.common.Utils.java

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

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

From source file:android.support.v7.view.menu.MenuPopup.java

/**
 * Measures the width of the given menu view.
 *
 * @param view The view to measure.//from ww  w.  j  a v a 2 s.  c  o m
 * @return The width.
 */
protected static int measureIndividualMenuWidth(ListAdapter adapter, ViewGroup parent, Context context,
        int maxAllowedWidth) {
    // Menus don't tend to be long, so this is more sane than it looks.
    int maxWidth = 0;
    View itemView = null;
    int itemType = 0;

    final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        final int positionType = adapter.getItemViewType(i);
        if (positionType != itemType) {
            itemType = positionType;
            itemView = null;
        }

        if (parent == null) {
            parent = new FrameLayout(context);
        }

        itemView = adapter.getView(i, itemView, parent);
        itemView.measure(widthMeasureSpec, heightMeasureSpec);

        final int itemWidth = itemView.getMeasuredWidth();
        if (itemWidth >= maxAllowedWidth) {
            return maxAllowedWidth;
        } else if (itemWidth > maxWidth) {
            maxWidth = itemWidth;
        }
    }

    return maxWidth;
}

From source file:net.mEmoZz.PopMovies.frags.DetailFragment.java

/**
 * Got this form here:/*  w  w w  .ja va  2s  .c om*/
 * http://stackoverflow.com/a/19311197/4043944
 */
public static void fix(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:rta.ae.sharekni.Display_My_Vehicles.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;// w  ww .j  a v a  2  s  .  co m
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(
                    new ViewGroup.LayoutParams(desiredWidth, RadioGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

From source file:com.jefftharris.passwdsafe.lib.view.GuiUtils.java

/**
 * Set the height of a ListView based on all of its children
 *///from   w  w w. ja v  a2  s  .  c o m
public static void setListViewHeightBasedOnChildren(final ListView listView) {
    final ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    // Defer measurement so listview is rendered to get its width
    listView.post(new Runnable() {
        @Override
        public void run() {
            int width = View.MeasureSpec.makeMeasureSpec(listView.getMeasuredWidth(), View.MeasureSpec.AT_MOST);
            int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int totalHeight = 0;
            int numItems = listAdapter.getCount();
            for (int i = 0; i < numItems; i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(width, height);
                totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight;
            if (numItems > 0) {
                params.height += listView.getDividerHeight() * (numItems - 1);
            }
            listView.setLayoutParams(params);
        }
    });
}

From source file:com.kiwiteam.nomiddleman.LargeGroupActivity.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;/*  w ww .  j a  va  2s  .  c  om*/

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }

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

From source file:com.nps.micro.view.TestsSectionFragment.java

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

    int totalHeight = 1;
    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);
    listView.requestLayout();
}

From source file:com.cerema.cloud2.ui.fragment.ShareFileFragment.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;//from  w  w w  . j  a v  a 2 s.c  o  m
    }
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0) {
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

From source file:com.mobicage.rogerthat.util.ui.UIUtils.java

public static boolean setListViewHeightBasedOnItems(ListView listView, int maxHeight) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return false;
    }//from   w  w  w .  j a v a2  s .c om
    int numberOfItems = listAdapter.getCount();

    // Get total height of all items.
    int totalItemsHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
        View item = listAdapter.getView(itemPos, null, listView);
        item.measure(desiredWidth, 0);
        totalItemsHeight += item.getMeasuredHeight();
    }

    // Get total height of all item dividers.
    int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1);

    // Set list height.
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalItemsHeight + totalDividersHeight;
    if (maxHeight > 0 && params.height > maxHeight) {
        params.height = maxHeight;
    }
    listView.setLayoutParams(params);
    listView.requestLayout();

    return true;
}