我正在使用FirebaseListAdapter并重写所需的onPopulateView方法以从实时数据库中收集数据,然后在UI中将适配器设置为ListView:

mMessageSummaryAdapter = new FirebaseListAdapter<MessageSummary> (getActivity(),
            MessageSummary.class, R.layout.message_list_item, mChatDbReference) {
        @Override
        protected void populateView(View v, MessageSummary msgSummary, int position) {
                final ImageView buddyImage = (ImageView) v.findViewById(R.id.small_image_view_messages);
                final TextView buddyName = (TextView) v.findViewById(R.id.contact_username);
....
mListView.setAdapter(mMessageSummaryAdapter);


使用我使用的其他适配器,可以检查进入其中的数据是否为空,并相应地设置视图的可见性,如果数据为空,则拉入一个空视图:

  if (foundRoutesList == null || foundRoutesList.size() == 0) {
        mEmptyView.setVisibility(View.VISIBLE);
        mRecyclerView.setVisibility(View.GONE);
    } else {
        mEmptyView.setVisibility(View.GONE);
        mAdapter = new SearchResultsAdapter(getContext(), foundRoutesList, this);
        mRecyclerView.setAdapter(mAdapter);
    }


据我了解,FirebaseListAdapter遍历找到的项目并逐个添加它们,因此如何测试结果是否为空,从而可以显示空文本视图?

最佳答案

要解决此问题,请在您的适配器类中重写FirebaseListAdapter的getCount()方法并检查是否为空:

if(getCount() == 0) {
    mEmptyView.setVisibility(View.VISIBLE);
    mRecyclerView.setVisibility(View.GONE);
} else {
    mEmptyView.setVisibility(View.GONE);
    mAdapter = new SearchResultsAdapter(getContext(), foundRoutesList, this);
    mRecyclerView.setAdapter(mAdapter);
}

关于android - FirebaseListAdapter emptyView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51486508/

10-09 06:15