如何获取 FirebaseListAdapter 中存在的元素计数?

我正在使用FirebaseListAdapter它ListView,它工作得很好。我有一个额外的要求来显示ListView. 我怎么做?


我通读了文档FirebaseListAdapter,发现有一个方法getCount()应该返回元素的计数。但它总是返回零。下面是我的代码。我缺少什么?


    mAppointmentAdapter = new FirebaseListAdapter<Appointment>(options) {


    @Override

        public void populateView(View view, Appointment appointment, int position) {


            TextView nameTextView = view.findViewById(R.id.app_patient_name_view);

            TextView reasonTextView = 


         ...         

        }


    };

    appointmentListView.setAdapter(mAppointmentAdapter);


    // Display count of appointments

    int mAppointmentsCount;

    mAppointmentsCount = mAppointmentAdapter.getCount();

    mAppointmentsCountView.setText(String.valueOf(mAppointmentsCount));

ListView需要适配器或适配器中的元素计数。


千巷猫影
浏览 106回答 1
1回答

眼眸繁星

我认为亚历克斯在这里有正确的想法:当您调用 时,很可能尚未从数据库加载项目mAppointmentAdapter.getCount(),因此它正确返回0。populateView正如 Alex 所说,该调用可能应该在内部,或者在适配器的onDataChanged方法中。每当 FirebaseUI 更新数据时都会onDataChanged调用 ,因此这是更新计数器的完美位置:@Overridepublic void onDataChanged() {  int mAppointmentsCount = mAppointmentAdapter.getCount();  mAppointmentsCountView.setText(String.valueOf(mAppointmentsCount));}如果这些知识不允许您解决问题,请编辑您的问题以显示该调用存在的时间/地点mAppointmentAdapter.getCount(),因为这使我们更有可能提供帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java