setText 不显示来自 Firebase 的数据
setText not showing data from Firebase
我的 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();
}
}
});
}
}
这是我来自 FirebaseJSON 的文件
{
"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"
}
}
}
根据您的意见,如果只获取“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 的任何值。也没有错误显示让我找到问题的原因。有人可以告诉我我错过的错误在哪里吗?任何帮助对我来说都非常有用。
这是我的 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();
}
}
});
}
}
这是我来自 FirebaseJSON 的文件
{
"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"
}
}
}
根据您的意见,如果只获取“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