Java Code Examples for android.widget.ListView#getAdapter()

The following examples show how to use android.widget.ListView#getAdapter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyUtils.java    From Huochexing12306 with Apache License 2.0 8 votes vote down vote up
/**
 * 设置ListView全部显示的高度
 * @param listView 要设置的ListView
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {
       //获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   //listAdapter.getCount()返回数据项的数目
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);  //计算子项View 的宽高
        totalHeight += listItem.getMeasuredHeight();  //统计所有子项的总高度
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    //listView.getDividerHeight()获取子项间分隔符占用的高度
    //params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
}
 
Example 2
Source File: TopLevelActivity.java    From HeadFirstAndroid with MIT License 6 votes vote down vote up
@Override
public void onRestart() {
    super.onRestart();
    try {
        StarbuzzDatabaseHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);
        db = starbuzzDatabaseHelper.getReadableDatabase();
        Cursor newCursor = db.query("DRINK",
                                    new String[] { "_id", "NAME"},
                                    "FAVORITE = 1",
                                    null, null, null, null);
        ListView listFavorites = (ListView)findViewById(R.id.list_favorites);
        CursorAdapter adapter = (CursorAdapter) listFavorites.getAdapter();
        adapter.changeCursor(newCursor);
        favoritesCursor = newCursor;
    } catch(SQLiteException e) {
        Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);
        toast.show();
    }
}
 
Example 3
Source File: OyUtils.java    From Oy with Apache License 2.0 6 votes vote down vote up
/**
 * * 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 = 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();
}
 
Example 4
Source File: RecentUnlocksCard.java    From WaniKani-for-Android with GNU General Public License v3.0 6 votes vote down vote up
public int setRecentUnlocksHeightBasedOnListView(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();

    if (listAdapter == null) {
        return (int) pxFromDp(550);
    } else {

        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 ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        totalHeight += mCardTitle.getMeasuredHeight();
        totalHeight += pxFromDp(16); // Add the paddings as well
        totalHeight += pxFromDp(48); // Add the more items button

        return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    }
}
 
Example 5
Source File: Tools.java    From android-tv-launcher with MIT License 6 votes vote down vote up
/**
 * @author sunglasses
 * @param listView
 * @category 计算listview高度
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {
	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();
	}

	ViewGroup.LayoutParams params = listView.getLayoutParams();
	params.height = totalHeight
			+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
	listView.setLayoutParams(params);
}
 
Example 6
Source File: ListViewUtil.java    From RxAndroidBootstrap with Apache License 2.0 6 votes vote down vote up
/**** 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 mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, listView);

        mView.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        totalHeight += mView.getMeasuredHeight();
        Log.w("HEIGHT" + i, String.valueOf(totalHeight));

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (mAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
 
Example 7
Source File: listviewutil.java    From Favorite-Android-Client with Apache License 2.0 6 votes vote down vote up
public static void setListViewHeightBasedOnChildren(ListView listView) {
    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() + 50;
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() -1));
    listView.setLayoutParams(params);
}
 
Example 8
Source File: NewsDetailActivity.java    From Social with Apache License 2.0 6 votes vote down vote up
/**
 * 使用自定义的Listview不用调用该方法
 * */
public void setListViewHeightBasedOnChildren(ListView listView) {
    // 获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
        // listAdapter.getCount()返回数据项的数目
        View listItem = listAdapter.getView(i, null, listView);
        // 计算子项View 的宽高
        listItem.measure(0, 0);
        // 统计所有子项的总高度
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
}
 
Example 9
Source File: FileSelectActivity.java    From aard2-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    FileSelectListAdapter adapter = (FileSelectListAdapter)l.getAdapter();
    File f = (File)adapter.getItem(position);
    if (f.isDirectory()) {
        this.setRoot(f);
    }
    else {
        Intent data = new Intent();
        data.putExtra(KEY_SELECTED_FILE_PATH, f.getAbsolutePath());
        setResult(RESULT_OK, data);
        finish();
    }
}
 
Example 10
Source File: InternalApisFragment.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
private final List<String> getSelectedReceiverIDs(final ListView listView) {
    final List<String> receiverIDs = new ArrayList<>();

    final Adapter adapter = listView.getAdapter();
    final SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
    for (int i = 0; i < adapter.getCount(); i++) {
        if (checkedItems.get(i)) {
            receiverIDs.add(((Receiver) adapter.getItem(i)).id);
        }
    }

    return receiverIDs;
}
 
Example 11
Source File: SwapWorkflowActivity.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    ListView peopleNearbyList = container.findViewById(R.id.list_people_nearby);
    if (peopleNearbyList != null) {
        ArrayAdapter<Peer> peopleNearbyAdapter = (ArrayAdapter<Peer>) peopleNearbyList.getAdapter();
        peopleNearbyAdapter.add((Peer) intent.getParcelableExtra(BluetoothManager.EXTRA_PEER));
    }
}
 
Example 12
Source File: RecycleActivity.java    From privacy-friendly-notes with GNU General Public License v3.0 5 votes vote down vote up
private void updateList() {
    ListView notesList = (ListView) findViewById(R.id.notes_list);
    CursorAdapter adapter = (CursorAdapter) notesList.getAdapter();
    String selection = DbContract.NoteEntry.COLUMN_TRASH + " = ?";
    String[] selectionArgs = { "1" };
    adapter.changeCursor(DbAccess.getCursorAllNotes(getBaseContext(), selection, selectionArgs));
}
 
Example 13
Source File: RefreshListView.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public boolean isEmpty() {
    ListView listView = getRefreshableView();
    if (listView == null) {
        return true;
    }
    ListAdapter adapter = listView.getAdapter();
    if (adapter == null) {
        return true;
    }
    return adapter.isEmpty();
}
 
Example 14
Source File: FileListFragment.java    From secrecy with Apache License 2.0 5 votes vote down vote up
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    FileListAdapter adapter = (FileListAdapter) l.getAdapter();
    if (adapter != null) {
        File file = (File) adapter.getItem(position);
        mPath = file.getAbsolutePath();
        mListener.onFileSelected(file);
    }
}
 
Example 15
Source File: ViewHelper.java    From DMusic with Apache License 2.0 5 votes vote down vote up
/**
 * 判断 ListView 是否已经滚动到底部
 *
 * @param listView 需要被判断的 ListView
 * @return
 */
public static boolean isListViewAlreadyAtBottom(ListView listView) {
    if (listView.getAdapter() == null || listView.getHeight() == 0) {
        return false;
    }

    if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1) {
        View lastItemView = listView.getChildAt(listView.getChildCount() - 1);
        if (lastItemView != null && lastItemView.getBottom() == listView.getHeight()) {
            return true;
        }
    }
    return false;
}
 
Example 16
Source File: UIUtils.java    From faveo-helpdesk-android-app with Open Software License 3.0 5 votes vote down vote up
public static boolean setListViewHeightBasedOnItems(ListView listView) {

        DrawerItemCustomAdapter listAdapter = (DrawerItemCustomAdapter) listView.getAdapter();
        if (listAdapter != null) {

            int numberOfItems = listAdapter.getCount();

            // Get total height of all items.
            int totalItemsHeight = 0;
            for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
                View item = listAdapter.getView(itemPos, null, listView);
                item.measure(0, 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;
            listView.setLayoutParams(params);
            listView.requestLayout();

            return true;

        } else {
            return false;
        }

    }
 
Example 17
Source File: Utils.java    From biermacht with Apache License 2.0 5 votes vote down vote up
/**
 * This method adjusts the height of the given listView to match the combined height of all if its
 * children and the dividers between list items.  This is used to set the height of the mash step
 * list such that it does not scroll, since it is encompassed by a ScrollView.
 *
 * @param listView
 *         ListView to adjust.
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {
  ListAdapter listAdapter = listView.getAdapter();
  if (listAdapter == null) {
    return;
  }

  int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
  int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
  View view = null;
  for (int i = 0; i < listAdapter.getCount(); i++) {
    view = listAdapter.getView(i, view, listView);

    view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

    if (i == 0) {
      view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
      //totalHeight += view.getMeasuredHeight();
    }

    totalHeight += view.getMeasuredHeight();
  }
  ViewGroup.LayoutParams params = listView.getLayoutParams();
  params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
  listView.setLayoutParams(params);
  listView.requestLayout();
}
 
Example 18
Source File: MyHomeListAdapter.java    From YiBo with Apache License 2.0 5 votes vote down vote up
public boolean refresh() {
	if (listNewBlogs == null || listNewBlogs.size() == 0) {
		return false;
	}

	addCacheToFirst(listNewBlogs);
	int offset = listNewBlogs.size();
	listNewBlogs.clear();

	ListView lvMicroBlog = (ListView)((Activity)context).findViewById(R.id.lvMicroBlog);
	if (lvMicroBlog == null) {
		return true;
	}
	Adapter adapter = lvMicroBlog.getAdapter();
	if (adapter instanceof HeaderViewListAdapter) {
		adapter = ((HeaderViewListAdapter)adapter).getWrappedAdapter();
	}
	if (adapter == this) {
		int position = lvMicroBlog.getFirstVisiblePosition();
		View view = lvMicroBlog.getChildAt(0);
		int y = 0;
		if (view != null && position >= 1) {
		    y = view.getTop();
		    //System.out.println("y:" + y + " position:" + position);
		    position += offset;
		    lvMicroBlog.setSelectionFromTop(position, y);
		}
	}

	return true;
}
 
Example 19
Source File: MainActivity.java    From a-sync-browser with Mozilla Public License 2.0 4 votes vote down vote up
private void navigateToFolder(FileInfo fileInfo) {
    Log.d("navigateToFolder", "BEGIN");
    if (indexBrowser.isRoot() && PathUtils.isParent(fileInfo.getPath())) {
        showAllFoldersListView(); //navigate back to folder list
    } else {
        if (fileInfo.isDirectory()) {
            indexBrowser.navigateTo(fileInfo);
            FileInfo newFileInfo=PathUtils.isParent(fileInfo.getPath())?indexBrowser.getCurrentPathInfo():fileInfo;
            if (!indexBrowser.isCacheReadyAfterALittleWait()) {
                Log.d("navigateToFolder", "load folder cache bg");
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected void onPreExecute() {
                        updateMainProgressBar(true,"open directory: " + (indexBrowser.isRoot() ? folderBrowser.getFolderInfo(indexBrowser.getFolder()).getLabel() : indexBrowser.getCurrentPathFileName()));
                    }

                    @Override
                    protected Void doInBackground(Void... voids) {
                        indexBrowser.waitForCacheReady();
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        Log.d("navigateToFolder", "cache ready, navigate to folder");
                        updateMainProgressBar(false,null);
                        navigateToFolder(newFileInfo);
                    }
                }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            } else {
                List<FileInfo> list = indexBrowser.listFiles();
                Log.i("navigateToFolder", "list for path = '" + indexBrowser.getCurrentPath() + "' list = " + list.size() + " records");
                Log.d("navigateToFolder", "list for path = '" + indexBrowser.getCurrentPath() + "' list = " + list);
                checkArgument(!list.isEmpty());//list must contain at least the 'parent' path
                ListView listView = (ListView) findViewById(R.id.main_folder_and_files_list_view);
                ArrayAdapter adapter = (ArrayAdapter) listView.getAdapter();
                adapter.clear();
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
                listView.setSelection(0);
                saveCurrentFolder();
                ((TextView) findViewById(R.id.main_header_folder_label)).setText(indexBrowser.isRoot()
                        ?folderBrowser.getFolderInfo(indexBrowser.getFolder()).getLabel()
                        :newFileInfo.getFileName());
            }
        } else {
            pullFile(fileInfo);
        }
    }
    Log.d("navigateToFolder", "END");
}
 
Example 20
Source File: Views.java    From hawkular-android-client with Apache License 2.0 4 votes vote down vote up
@UiThread
public static int measureHeight(@NonNull ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();

    if (listAdapter == null) {
        return 0;
    }

    int listItemsCount = listAdapter.getCount();

    int listItemViewsHeight = 0;

    for (int listItemViewPosition = 0; listItemViewPosition < listItemsCount; listItemViewPosition++) {
        View listItemView = listAdapter.getView(listItemViewPosition, null, listView);

        listItemViewsHeight += Views.measureHeight(listItemView);
    }

    int listDividerViewsHeight = listView.getDividerHeight() * (listItemsCount - 1);

    return listItemViewsHeight + listDividerViewsHeight;
}