简体   繁体   English

如何将 Firebase Firestore 与 Fragment 一起使用?

[英]How to use Firebase Firestore with Fragments?

I created a bottom nav menu, and I have fragments for each screen and I really don't know how to inject some values into my layout.我创建了一个底部导航菜单,每个屏幕都有片段,我真的不知道如何将一些值注入到我的布局中。 I mean, I have a settings fragment like this :我的意思是,我有一个这样的设置片段:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".SettingsFragment"
    android:background="@color/homeFragmentBg"

    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="60dp"

        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"

            android:elevation="5dp"

            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@color/colorAccent"
                android:padding="20dp">

              
                <TextView
                    android:id="@+id/settings_fullName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Name From Firebase"
                    android:textSize="30sp"
                    android:fontFamily="@font/muli_black"
                    android:textColor="@color/colorPrimary"
                    android:includeFontPadding="false"
                    android:layout_toRightOf="@id/settings_logo"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"


                    />
            </RelativeLayout>

           
        </LinearLayout>
    </ScrollView>

</FrameLayout>

and I have this TextView with text "Name From Firebase", but I can't find findViewById in my fragment java file, and I don't know if I did ok when I fetched the database for (real time updates)..我有这个带有文本“来自 Firebase 的名称”的 TextView,但我在片段 java 文件中找不到 findViewById,而且我不知道当我获取数据库(实时更新)时我是否做得好..

public class SettingsFragment extends Fragment {


    private FirebaseAuth mAuth;
    private FirebaseFirestore db;

    TextView name;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        mAuth = FirebaseAuth.getInstance();
        db = FirebaseFirestore.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();
        
        
        
        DocumentReference docRef = db.collection("users").document(user.getUid());

        docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
                if(error!=null) {
                    System.err.println("Listen Failed:" + error);
                    return;
                }
                if(value != null && value.exists()) {
                    //Here I should assign what I get from DB to Layout??
                } else {
                    System.out.print("curent data: null");
                }
            }
        });

        return inflater.inflate(R.layout.fragment_settings, container, false);


    }
}

Can you please suggest me a good tutorial how can I do that?你能给我推荐一个好的教程我该怎么做? Or explain me the process, I don't want code, I need to learn.或者给我解释一下过程,我不要代码,我要学习。 Thank you very much!非常感谢!

@Override public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) { @Override public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {

   DocumentReference docRef = db.collection("users").document(user.getUid());

    docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
            if(error!=null) {
                System.err.println("Listen Failed:" + error);
                return;
            }
            if(value != null && value.exists()) {
                //Here I should assign what I get from DB to Layout??
       //TODO you update your respected data here with the help of defining a method.
            } else {
                System.out.print("curent data: null");
            }
        }
    });

} }

Or want to learn about this check on this tutorial-- https://www.zoftino.com/firebase-cloud-firestore-databse-tutorial-android-example或者想在本教程中了解此检查 - https://www.zoftino.com/firebase-cloud-firestore-databse-tutorial-android-example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Firebase Firestore 中使用查询 - How to use Query in Firebase Firestore 如何使用Firestore在片段中使用RecyclerView-Android - How can I use RecyclerView in fragments using Firestore - Android 如何在 Firebase Firestore 查询中使用 whereEqualTo() 和 orderBy()? - How to use whereEqualTo() and orderBy() in a Firebase Firestore query? 如何在片段中使用 FirebaseRecycle Adapter 来显示 Firebase 数据库? - How to use FirebaseRecycle Adapter in fragments to show Firebase Database? 如何在不使用firebase身份验证的情况下编写firestore数据库安全规则 - How to write firestore database security rule while not use firebase authentication 如何使用带有清晰代码的Firebase Cloud Firestore引用? - How to use Firebase Cloud Firestore references with clean code? 如何在Android java中的FireBase Firestore中使用像“AND”这样的SQL - How to use SQL like “AND” in FireBase Firestore in Android java 如何正确使用 firebase-firestore 异步调用 - How to properly use firebase-firestore async calls 如何将 firebase firestore 用于我的桌面应用程序? - How do I use firebase firestore for my desktop app? 如何将SwipeRefreshLayout与片段一起使用 - how to use SwipeRefreshLayout with Fragments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM