我正在使用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遍历找到的项目并逐个添加它们,因此,如何测试结果是否为空,从而可以显示空的文本视图?
答案 0 :(得分:1)
要解决此问题,请在您的适配器类中重写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);
}