我想从Firestore文档中提取数据。getdata ()
我要做的是:
从Firestore获取数据并在Java文件中提取该数据,并在文本视图中显示提取的数据
My Full Android Java Code
package com.verify.humanverify.ui.dashboard;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.verify.humanverify.R;
import com.verify.humanverify.ui.notifications.NotificationsViewModel;
import java.util.List;
public class DashboardFragment extends Fragment {
private DashboardViewModel dashboardViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel =
new ViewModelProvider(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
final TextView textView = root.findViewById(R.id.aadharnoni);
dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(getActivity());
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("Details").whereEqualTo("Person E-Mail", acct.getEmail()).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("Data", document.getId() + " => " + document.getData());
textView.setText(document.getData()+document.getId());
}
} else {
Log.w("Error", "Error getting documents.", task.getException());
}
}
});
}
});
return root;
}
}
来自Firestore的主要数据
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("Data", document.getId() + " => " + document.getData());
textView.setText(document.getData()+document.getId());
}
} else {
Log.w("Error", "Error getting documents.", task.getException());
}
}
我想从这里提取数据
Log.d("Data", document.getId() + " => " + document.getData());
textView.setText(document.getData()+document.getId());
从这个方法获取数据document.getData()
My XML Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.dashboard.DashboardFragment"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/user_image"
android:layout_width="203dp"
android:layout_height="208dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.12"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/aadharnoni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/denk_one"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/user_image"
app:layout_constraintVertical_bias="0.076"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/aadhar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/denk_one"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aadharno"
app:layout_constraintVertical_bias="0.076"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/denk_one"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aadhar"
app:layout_constraintVertical_bias="0.076"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/denk_one"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/gender"
app:layout_constraintVertical_bias="0.076"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
我必须显示数据的文本视图
<TextView
android:id="@+id/aadharnoni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/denk_one"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/user_image"
app:layout_constraintVertical_bias="0.076"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/aadhar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/denk_one"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aadharno"
app:layout_constraintVertical_bias="0.076"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/denk_one"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aadhar"
app:layout_constraintVertical_bias="0.076"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/denk_one"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/gender"
app:layout_constraintVertical_bias="0.076"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
This Is The Collection Which I Am Getting
D/Data: UN5RqHsimOvXAMF1sTtY => {Person Photo URL=https://lh3.googleusercontent.com/a-/AOh14GhQbVE3TmhXdpi-vxYSa5QuUxOXVQ95zYotBSzv=s96-c, Person Id=112019185671391718103, Aadhar=XXXXXX, Person Given Name=Krishna, Driving License=XXXXXX, Person Name=Krishna Wadhwani, Person Family Name=Wadhwani, Person E-Mail=kwswhwmw@gmail.com, Date Of Birth=XXXXXX}
我想在不同的TextViews中显示这些字段例如
Person E-Mail In E-Mail Text View
And So On…
来自THE KOOL WINGS的提前感谢
如果想要单字段数据。那就用.get(/*path*/)
吧
您可以尝试这样获取Name
:
Log.e("name", document.get("Person Given Name").toString());