[英]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.