android - 有没有办法根据 firebase 数据库中的子项数量动态更改 Android 中的选项卡数量?

标签 android firebase firebase-realtime-database tabs

我必须创建一个包含选项卡数量的项目,该数量取决于 firebase 数据库中的子项数量。

如果一个新的 child 被删除或添加到数据库中,我需要减少和增加选项卡的数量。

我已经设法获得 Firebase 数据库中的子级数量,但我无法将它传递给决定选项卡数量的函数 (getcount())。

我从中检索 Firebase 数据的类:

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommunitiesFragment extends Fragment {
    private static int commonVariable;
    private DatabaseReference mDatabaseReference;


    public CommunitiesFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.community_tab, container, false);
        mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("COMMUNITIES");
            Log.e("TESTApp", "The value is checked");
            mDatabaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    commonVariable = (int) dataSnapshot.getChildrenCount();
                    Log.e("COMMON-VALUE", commonVariable + "");

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

            return v;
    }

     static int tabNumber(){
        int a = CommunitiesFragment.commonVariable;
        Log.e("Tab Function Value:",a+"");
        return a;
    }

}

负责创建选项卡的选项卡适配器类:

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;


public class CommunityAdapter extends FragmentStatePagerAdapter {
    public CommunityAdapter(@NonNull FragmentManager fm) {
        super(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }


    @NonNull
    @Override
    public Fragment getItem(int position) {
        CommunitiesFragment communitiesFragment=new CommunitiesFragment();
        Bundle bundle=new Bundle();
        position=position+1;
        bundle.putString("message","Success"+position);
        communitiesFragment.setArguments(bundle);

        return communitiesFragment;
    }

    @Override
    public int getCount(){
        int TabNumber = CommunitiesFragment.tabNumber();
        Log.e("InGetCountFunction",TabNumber+"");
        return TabNumber;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        position=position+1;
        return "COMMUNITY "+position ;
    }

    public class CommunityViewHolder {

    }

}

Firebase 数据库的图片

Please Refer if needed

我希望选项卡数量等于社区子项的数量。 下一步是在每个子项的单独选项卡中显示嵌套属性

目前 TabNumber 函数没有提供正确的值,它只显示 0。 我还觉得可能是 firebase 的 onDataChanege 函数在应用程序启动时没有被触发

错误的 Logcat:

    2019-12-22 20:13:43.670 1408-1460/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2019-12-22 20:13:52.354 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.354 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:52.355 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.355 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:52.362 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.362 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:52.364 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.364 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:52.412 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.412 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:54.119 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:54.119 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:54.783 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:54.783 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:54.971 1595-1608/? E/memtrack: Couldn't load memtrack module
2019-12-22 20:13:55.265 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:55.265 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:55.799 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:55.799 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:56.263 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:56.263 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:14:04.765 1595-1608/? E/memtrack: Couldn't load memtrack module

最佳答案

数据是从 Firebase 异步加载的,因为从服务器获取数据可能需要一些时间。您的主代码不会在等待数据时阻塞应用程序,而是继续运行,并且(例如)初始化 fragment 。此时您的 onDataChange 尚未运行,您的 commonVariable 仍设置为默认值。只有当数据可用时,您的 onDataChange 才会被调用,并且 commonVariable 被设置为正确的值。但此时 fragment 已经呈现了错误的 commonVariable 值。

解决方案是将任何需要数据库数据的代码放在 onDataChange 中或从那里调用它。有关如何执行这两项操作的示例,请参阅 getContactsFromFirebase() method return an empty list

关于android - 有没有办法根据 firebase 数据库中的子项数量动态更改 Android 中的选项卡数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59445288/

相关文章:

firebase - 在 firebase 中使用 REST API 添加 child

ios - 用来自服务器的数据填充 UIPageViewController?

firebase - 我想在我的Flutter应用程序中从Firebase数据库中获取当前用户数据

java - 在 recyclerView 中显示元素列表失败

firebase - 如何在忽略现有功能的同时部署 Cloud Functions?

c# - c#中的Firebase(API推荐)

reactjs - firebase.initializeApp 不是函数

android - 解锁关键组件

java - 倒数计时器在手机关闭或应用程序被杀死时仍然运行

安卓应用交互