setText 不显示来自 Firebase 的数据

Posted

技术标签:

【中文标题】setText 不显示来自 Firebase 的数据【英文标题】:setText not showing data from Firebase 【发布时间】:2021-09-20 21:12:37 【问题描述】:

我的 setText 没有显示来自 Firebase 的任何值。也没有显示错误让我找到问题的原因。有人可以告诉我我错过的错误在哪里吗?任何帮助都会对我非常有用。

这是我的 XML 编码。

fragment_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/frameLayout"
    android:layout_
    android:layout_
    tools:context=".DashboardFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_
        android:animateLayoutChanges="false">


        <TextView
            android:id="@+id/textView2"
            android:layout_
            android:layout_
            android:layout_marginTop="100dp"
            android:text="BOOKING ROOM LIST"
            android:textColor="@color/black"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/esearch"
            android:layout_
            android:layout_
            android:layout_marginTop="36dp"
            android:background="@drawable/custom_input"
            android:drawableStart="@drawable/ic_baseline_search_24"
            android:drawablePadding="12dp"
            android:hint="Room No"
            android:padding="12dp"
            android:paddingStart="12dp"
            android:paddingLeft="20dp"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />


        <Button
            android:id="@+id/readdataBtn"
            android:layout_
            android:layout_
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="80dp"
            android:backgroundTint="@color/teal_200"
            android:text="SEARCH"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.49"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/esearch" />

        <View
            android:id="@+id/view3"
            android:layout_
            android:layout_
            android:layout_marginTop="20dp"
            android:background="@color/black"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/readdataBtn" />

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_
            android:layout_
            android:layout_marginTop="45dp"
            android:orientation="horizontal"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/readdataBtn">

            <TextView
                android:layout_
                android:layout_
                android:layout_marginStart="20dp"
                android:text="MATRIX NO:"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvmatrix"
                android:layout_
                android:layout_
                android:layout_marginStart="5dp"
                android:textColor="@color/black"
                android:text=""
                android:textSize="20sp" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_
            android:layout_
            android:layout_marginTop="105dp"
            android:orientation="horizontal"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/readdataBtn">

            <TextView
                android:layout_
                android:layout_
                android:layout_marginStart="20dp"
                android:text="ROOM NO :"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvroom"
                android:layout_
                android:layout_
                android:layout_marginStart="5dp"
                android:text=""
                android:textColor="@color/black"
                android:textSize="20sp" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_
            android:layout_
            android:layout_marginTop="34dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout5">

            <TextView
                android:layout_
                android:layout_
                android:layout_marginStart="20dp"
                android:text="DATE : "
                android:textColor="@color/black"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvdate"
                android:layout_
                android:layout_
                android:layout_marginStart="5dp"
                android:text=""
                android:textColor="@color/black"
                android:textSize="20sp" />


        </LinearLayout>

        <LinearLayout
            android:layout_
            android:layout_
            android:layout_marginTop="27dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout4">

            <TextView
                android:layout_
                android:layout_
                android:layout_marginStart="20dp"
                android:text="TIME : "
                android:textColor="@color/black"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvtime"
                android:layout_
                android:layout_
                android:layout_marginStart="5dp"
                android:text=""
                android:textColor="@color/black"
                android:textSize="20sp" />


        </LinearLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

这是我的java代码。

DasboardFragment.java

public class DashboardFragment extends Fragment
    
        FragmentDashboardBinding binding;
        DatabaseReference reference;
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
    
            View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
            binding = FragmentDashboardBinding.inflate(getLayoutInflater());
            FragmentDashboardBinding binding= FragmentDashboardBinding.inflate(inflater);
            binding.readdataBtn.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    Toast.makeText(getContext(),"PLease Enter Room Number",Toast.LENGTH_SHORT);
    
                    String roomnum = binding.esearch.getText().toString();
                    if (!roomnum.isEmpty())
    
                        readData(roomnum);
                    else
    
                        Toast.makeText(getContext(),"PLease Enter Room Number",Toast.LENGTH_SHORT).show();
                    
    
                
    
    
    
            );
            return binding.getRoot();
    
        
    
        private void readData(String roomnum) 
    
            reference = FirebaseDatabase.getInstance().getReference("Booking");
            reference.child(roomnum).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() 
                @Override
                public void onComplete(@NonNull Task<DataSnapshot> task) 
    
                    if (task.isSuccessful())
    
                        if (((task.getResult()).exists()))
    
    
                            DataSnapshot dataSnapshot = task.getResult();
                            String matrixNum = String.valueOf(dataSnapshot.child("Matrix").getValue(Booking.class));
                            String date = String.valueOf(dataSnapshot.child("Date").getValue());
                            String room = String.valueOf(dataSnapshot.child("Room").getValue());
                            String time = String.valueOf(dataSnapshot.child("Time").getValue());
                            binding.tvdate.setText(date);
                            binding.tvmatrix.setText(matrixNum);
                            binding.tvroom.setText(room);
                            binding.tvtime.setText(time);
                            Toast.makeText(getActivity(),"Successfully Load",Toast.LENGTH_SHORT).show();
    
    
                        else 
    
                            Toast.makeText(getActivity(),"Room Doesn't Exist",Toast.LENGTH_SHORT).show();
    
                        
    
    
                    else 
    
                        Toast.makeText(getActivity(),"Something Went Wrong",Toast.LENGTH_SHORT).show();
                    
    
    
                
            );
    
        
    
    

这是我来自 Firebase 的 JSON 文件


  "Booking" : 
    "123456" : 
      "Date" : "22/4",
      "Matrix" : 124,
      "Room" : 2,
      "Time" : "11.00 am - 12.00 pm"
    ,
   
    "BK20" : 
      "Date" : "22/5",
      "Matrix" : 123,
      "Room" : 3,
      "Time" : "12.00 pm - 1.00 pm"
    ,
    "BK21" : 
      "Date" : "1/7/21",
      "Matrix" : 201302000,
      "Room" : "BK21",
      "Time" : "1.00 pm - 2.00 pm"
    
  

【问题讨论】:

是否显示任何 Toast 消息? @AlexMamo yes.. 显示 Toast 消息“成功加载”。 请编辑您的问题并将您的数据库结构添加为 JSON 文件。您可以通过单击 Firebase Console 的溢出菜单 (⠇) 中的导出 JSON 来简单地获取它。 @AlexMamo 但 setText 没有显示任何内容。 @AlexMamo 我已经包含了我的数据库结构。 【参考方案1】:

根据你的cmets,只获取“BK20”的内容,请使用以下方法:

private void readData(String bookingNumber) 
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference bookingNumberRef = rootRef.child("Booking").child(bookingNumber);
    bookingNumberRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() 
        @Override
        public void onComplete(@NonNull Task<DataSnapshot> task) 
            if (task.isSuccessful()) 
                DataSnapshot snapshot = task.getResult();
                String date = snapshot.child("Date").getValue(String.class);
                long matrix = snapshot.child("Matrix").getValue(Long.class);
                long room = snapshot.child("Room").getValue(Long.class);
                String time = snapshot.child("Time").getValue(String.class);
                Log.d("TAG", date + ", " + matrix + ", " + room + ", " + time);
             else 
                Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
            
        
    );

然后开始:

readData("BK20");

logcat 中的结果将是:

22/5, 123, 3, 12.00 pm - 1.00 pm

【讨论】:

以上是关于setText 不显示来自 Firebase 的数据的主要内容,如果未能解决你的问题,请参考以下文章

离子 |未显示来自 Firebase 的结果

列表视图不显示来自firebase数据库的任何内容

Flutter:如何显示来自 Firebase 的文本?

如何在反应js中显示来自firebase存储的所有图像

如何在我的应用程序上显示来自 Firebase 存储的图像?

IONIC - 如何显示来自firebase存储的图像而无需下载它[关闭]