【问题标题】:Is there is any way to implement FirebaseListAdapter in a Tab view?有什么方法可以在选项卡视图中实现 FirebaseListAdapter?
【发布时间】:2017-06-21 16:08:59
【问题描述】:

我在活动中创建了一个选项卡视图。现在我想在每个选项卡中显示要从 Firebase 检索的列表项。

我可以检索和显示活动中的数据。但我不知道如何在片段中使用FirebaseListAdapterFirebaseListAdapter 需要第一个参数作为 Activity,但我怎样才能将 Fragment 作为 Activity 传递?

childRef = databaseReference.child(str);
FirebaseListAdapter<Leave> firebaseListAdapter = new FirebaseListAdapter<Leave>(
            this,
            Leave.class,
            R.layout.leavelistview,
            childRef
    ) {
        @Override
        protected void populateView(View v, Leave model, int position) {
            ((TextView)v.findViewById(R.id.lv_staffname)).setText(model.getName());
            ((TextView)v.findViewById(R.id.lv_type)).setText(model.getType());
            ((TextView)v.findViewById(R.id.lv_duration)).setText(model.getDuration());
        }
    };
    listView.setAdapter(firebaseListAdapter);

这是我在活动中使用的代码。但是在片段中实现它会给我一个错误,因为我们不能为片段传递this

【问题讨论】:

    标签: android listview firebase-realtime-database firebaseui tabview


    【解决方案1】:

    您可以在片段上调用getActivity() 将其传递给适配器。像这样:

    FirebaseListAdapter<Leave> firebaseListAdapter = new FirebaseListAdapter<Leave>(
                getActivity(),
                Leave.class,
                R.layout.leavelistview,
                childRef
        ) {
            @Override
            protected void populateView(View v, Leave model, int position) {
                ((TextView)v.findViewById(R.id.lv_staffname)).setText(model.getName());
                ((TextView)v.findViewById(R.id.lv_type)).setText(model.getType());
                ((TextView)v.findViewById(R.id.lv_duration)).setText(model.getDuration());
            }
        };
    

    【讨论】:

    • 它仍然给我错误说预期参数是android.app.activity
    • 尝试使用getActivity()
    • 你能检查一下这个问题吗? stackoverflow.com/questions/42040064/…
    • @MbaiMburu 您可能在活动上使用适配器而不是在片段中。请改用MainActivity.this。 (将 MainActivity 替换为您的活动名称)
    猜你喜欢
    • 1970-01-01
    • 2011-03-14
    • 2022-12-15
    • 2012-08-07
    • 2019-06-01
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多