【发布时间】: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_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".DashboardFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="628dp"
android:animateLayoutChanges="false">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="360dp"
android:layout_height="50dp"
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_width="261dp"
android:layout_height="59dp"
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_width="match_parent"
android:layout_height="1dp"
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_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/readdataBtn">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="143dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:textColor="@color/black"
android:text=""
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="105dp"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/readdataBtn">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="177dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text=""
android:textColor="@color/black"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="DATE : "
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvdate"
android:layout_width="164dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text=""
android:textColor="@color/black"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="TIME : "
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvtime"
android:layout_width="175dp"
android:layout_height="wrap_content"
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 我已经包含了我的数据库结构。
标签: java android firebase android-fragments firebase-realtime-database