Example usage for android.widget ListAdapter getView

List of usage examples for android.widget ListAdapter getView

Introduction

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

Prototype

View getView(int position, View convertView, ViewGroup parent);

Source Link

Document

Get a View that displays the data at the specified position in the data set.

Usage

From source file:com.lgallardo.qbittorrentclient.TorrentDetailsFragment.java

/**
 * *//from   ww w .jav a2s . c o  m
 * Method for Setting the Height of the ListView dynamically. Hack to fix
 * the issue of not showing all the items of the ListView when placed inside
 * a ScrollView
 * **
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) {

        long numOfLines = 1;
        view = listAdapter.getView(i, view, listView);

        if (i == 0) {
            view.setLayoutParams(new LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        }

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);

        TextView file = (TextView) view.findViewById(R.id.file);
        TextView percentage = (TextView) view.findViewById(R.id.percentage);
        ProgressBar progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);

        if (view.getMeasuredWidth() > desiredWidth) {

            double viewWidthLong = Double.valueOf(view.getMeasuredWidth());
            double desiredWidthLong = Double.valueOf(desiredWidth);

            numOfLines = Math.round(viewWidthLong / desiredWidthLong) + 1;

            totalHeight += (file.getMeasuredHeight() * numOfLines) + percentage.getMeasuredHeight()
                    + progressBar1.getMeasuredHeight();

        } else {
            totalHeight += view.getMeasuredHeight();
        }

    }

    LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

From source file:util.Utils.java

/**
 * gridview//  w w w  . j a v a 2 s  .c o  m
 *
 * @param listView
 */
public static void setListViewHeightBasedOnChildren(GridView listView) {
    // ?listviewadapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    // 
    int col = listView.getNumColumns();
    int totalHeight = 0;
    // i?4listAdapter.getCount()?4 item
    // listAdapter.getCount()?8
    for (int i = 0; i < listAdapter.getCount(); i += col) {
        // ?listview?item
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        // ?item
        totalHeight += listItem.getMeasuredHeight();
    }
    totalHeight += listView.getVerticalSpacing();

    // ?listview?
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    // 
    params.height = totalHeight;
    // margin
    //        ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10);
    // ?
    listView.setLayoutParams(params);
}

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  ww. ja  va  2 s.  c o  m*/
    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;
}

From source file:com.bamobile.fdtks.util.Tools.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;//w w w.ja  va2 s.  com

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

From source file:com.lgallardo.youtorrentcontroller.TorrentDetailsFragment.java

/**
 * *//from  w  ww.  j a v  a  2s . c  o m
 * Method for Setting the Height of the ListView dynamically. Hack to fix
 * the issue of not showing all the items of the ListView when placed inside
 * a ScrollView
 * **
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) {

        long numOfLines = 1;
        view = listAdapter.getView(i, view, listView);

        if (i == 0) {
            view.setLayoutParams(new LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        }

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);

        TextView file = (TextView) view.findViewById(R.id.file);
        TextView info = (TextView) view.findViewById(R.id.info);

        if (view.getMeasuredWidth() > desiredWidth) {

            double viewWidthLong = Double.valueOf(view.getMeasuredWidth());
            double desiredWidthLong = Double.valueOf(desiredWidth);

            numOfLines = Math.round(viewWidthLong / desiredWidthLong) + 1;

            totalHeight += file.getMeasuredHeight() * numOfLines + info.getMeasuredHeight();

        } else {
            totalHeight += view.getMeasuredHeight();
        }

    }

    LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

From source file:com.lolay.android.ui.LolayBaseFragmentActivity.java

/**
 * Thanks to http://nex-otaku-en.blogspot.com/2010/12/android-put-listview-in-scrollview.html
 * WARNING: Only works with Linear Layouts or you'll get a NullPointerException in the measure code
 *//*from   ww w.  jav a  2  s .co  m*/
public void listViewSetHeightFromChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        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.moonpi.tapunlock.MainActivity.java

public static void updateListViewHeight(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();

    if (myListAdapter == null)
        return;/*from  w  w w  .  jav  a  2s  . c  o m*/

    // Get listView height
    int totalHeight = myListView.getPaddingTop() + myListView.getPaddingBottom();
    int adapterCount = myListAdapter.getCount();

    for (int i = 0; i < adapterCount; i++) {
        View listItem = myListAdapter.getView(i, null, myListView);

        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    // Change height of listView
    ViewGroup.LayoutParams paramsList = myListView.getLayoutParams();
    paramsList.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(paramsList);
}

From source file:com.bamalearn.bamalearnburmese.DrawerMainActivity.java

public void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;/*from w ww .ja  va 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 AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,
                    AbsListView.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:at.alladin.rmbt.android.views.ResultQoSDetailView.java

@SuppressWarnings("unchecked")
@Override//ww  w.java  2 s  .c  o m
public void taskEnded(JSONArray result) {
    System.out.println("ResultQoSDetail taskEnded");
    this.testResult = result;

    if (resultFetchEndTaskListener != null) {
        resultFetchEndTaskListener.taskEnded(result);
    }

    ProgressBar resultProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
    TextView resultTextView = (TextView) view.findViewById(R.id.info_text);

    try {
        results = new QoSServerResultCollection(result);

        View successList = view.findViewById(R.id.qos_success_list);

        List<HashMap<String, String>> itemList = new ArrayList<HashMap<String, String>>();
        int index = 0;
        for (QoSTestResultEnum type : QoSTestResultEnum.values()) {
            if (results.getQoSStatistics().getTestCounter(type) > 0) {
                HashMap<String, String> listItem = new HashMap<String, String>();
                listItem.put("name", ConfigHelper.getCachedQoSNameByTestType(type, activity));
                listItem.put("type_name", type.toString());
                listItem.put("index", String.valueOf(index++));
                itemList.add(listItem);
            }
        }

        ListAdapter valueList = new SimpleAdapter(activity, itemList, R.layout.qos_category_list_item,
                new String[] { "name" }, new int[] { R.id.name });

        resultProgressBar.setVisibility(View.GONE);

        if (valueList.getCount() > 0) {

            //in case the view will change again:
            if (ListView.class.isAssignableFrom(successList.getClass())) {
                ((ListView) successList).setAdapter(valueList);
                ((ListView) successList).setOnItemClickListener(this);
            } else {
                ViewGroup vgList = (ViewGroup) successList;
                for (int i = 0; i < valueList.getCount(); i++) {
                    View v = valueList.getView(i, null, null);

                    QoSTestResultEnum key = QoSTestResultEnum
                            .valueOf(((HashMap<String, String>) valueList.getItem(i)).get("type_name"));
                    if (results.getQoSStatistics().getFailureCounter(key) > 0) {
                        ImageView img = (ImageView) v.findViewById(R.id.status);
                        img.setImageResource(R.drawable.traffic_lights_red);
                    }

                    TextView status = (TextView) v.findViewById(R.id.qos_type_status);
                    status.setText((results.getQoSStatistics().getTestCounter(key)
                            - results.getQoSStatistics().getFailedTestsCounter(key)) + "/"
                            + results.getQoSStatistics().getTestCounter(key));

                    v.setOnClickListener(this);
                    v.setTag(valueList.getItem(i));
                    vgList.addView(v);
                }
            }

            successList.invalidate();

            resultTextView.setVisibility(View.GONE);
            successList.setVisibility(View.VISIBLE);
        } else {
            resultTextView.setText(R.string.result_qos_error_no_data_available);
        }

    } catch (Throwable t) {
        resultTextView.setText(R.string.result_qos_error_no_data_available);
        resultProgressBar.setVisibility(View.GONE);
        t.printStackTrace();
    }
}

From source file:cx.ring.fragments.CallListFragment.java

public void setGridViewHeight(GridView gridView, LinearLayout llMain) {
    ListAdapter listAdapter = gridView.getAdapter();
    if (listAdapter == null) {
        return;//w  ww  .j  av a2 s . c o m
    }

    int totalHeight = 0;
    int firstHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(gridView.getWidth(), View.MeasureSpec.AT_MOST);

    int rows = (listAdapter.getCount() + gridView.getNumColumns() - 1) / gridView.getNumColumns();

    for (int i = 0; i < rows; i++) {
        if (i == 0) {
            View listItem = listAdapter.getView(i, null, gridView);
            listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            firstHeight = listItem.getMeasuredHeight();
        }
        totalHeight += firstHeight;
    }

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llMain.getLayoutParams();

    params.height = (int) (totalHeight
            + (getResources().getDimension(R.dimen.contact_vertical_spacing) * (rows - 1)
                    + llMain.getPaddingBottom() + llMain.getPaddingTop()));
    llMain.setLayoutParams(params);
    mHeader.requestLayout();
}