FirebaseListAdapter为空视图



我正在使用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);

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

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

相关内容

  • 没有找到相关文章

最新更新