我是 Android Studio Java 编码的新手,我遇到了一个问题,即当我使用 FirebaseRecycleAdapter 从 Firebase 检索数据时,当我的 Firebase 有多个结果时,它只显示一个输出结果。以下是我的 Firebase 途径和结果,Firebase Pathway and Data
Output Results
下面是我的代码,这是这个特定函数的MainActivity
package com.example.fixit;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class COrderHistory extends AppCompatActivity {
public RecyclerView recyclerView;
public RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference requests;
FirebaseRecyclerAdapter<CBooking, COrderViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c_order_history);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
database = FirebaseDatabase.getInstance();
requests = database.getReference("Customer");
recyclerView = (RecyclerView)findViewById(R.id.order_history);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
loadOrders(Common.currentUser.getEmail());
}
private void loadOrders(String email) {
FirebaseRecyclerOptions<CBooking> options = new
FirebaseRecyclerOptions.Builder<CBooking>()
.setQuery(requests.child("booking"), CBooking.class).build();
adapter = new FirebaseRecyclerAdapter<CBooking, COrderViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull COrderViewHolder holder, int position, @NonNull CBooking model) {
holder.txtOrderName.setText(model.getA_fullName());
holder.txtOrderAddress.setText(model.getD_address());
holder.txtOrderHp.setText(model.getB_contactNo());
holder.txtOrderDate.setText(model.getE_date());
holder.txtOrderServices.setText(model.getG_serviceType());
}
@NonNull
@Override
public COrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_history_layout, parent, false);
return new COrderViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
这是上面Java代码的xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".COrderHistory">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/order_history"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是cardView的xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:orientation="horizontal"
android:layout_gravity="center_vertical">
<LinearLayout
android:orientation="vertical"
android:layout_weight="9"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/order_name"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="bold"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/order_contactNumber"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="bold"
android:text="Contact Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/order_address"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="bold"
android:text="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/order_date"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="bold"
android:text="Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/order_services"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="bold"
android:text="Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
这是Model类的代码
package com.example.fixit;
public class CBooking {
private String a_fullName;
private String b_contactNo;
private String c_email;
private String d_address;
private String e_date;
private String f_time;
private String g_serviceType;
public CBooking() {
}
public CBooking(String a_fullName, String b_contactNo, String c_email, String d_address, String e_date, String f_time, String g_serviceType) {
this.a_fullName = a_fullName;
this.b_contactNo = b_contactNo;
this.c_email = c_email;
this.d_address = d_address;
this.e_date = e_date;
this.f_time = f_time;
this.g_serviceType = g_serviceType;
}
public String getA_fullName() {
return a_fullName;
}
public void setA_fullName(String a_fullName) {
this.a_fullName = "Derek";
}
public String getB_contactNo() {
return b_contactNo;
}
public void setB_contactNo(String b_contactNo) {
this.b_contactNo = b_contactNo;
}
public String getC_email() {
return c_email;
}
public void setC_email(String c_email) {
this.c_email = c_email;
}
public String getD_address() {
return d_address;
}
public void setD_address(String d_address) {
this.d_address = d_address;
}
public String getE_date() {
return e_date;
}
public void setE_date(String e_date) {
this.e_date = e_date;
}
public String getF_time() {
return f_time;
}
public void setF_time(String f_time) {
this.f_time = f_time;
}
public String getG_serviceType() {
return g_serviceType;
}
public void setG_serviceType(String g_serviceType) {
this.g_serviceType = g_serviceType;
}
}
这是 ViewHolder 的 Java 代码
package com.example.fixit;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class COrderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtOrderName, txtOrderHp, txtOrderAddress, txtOrderDate, txtOrderServices;
private ItemClickListener itemClickListener;
public COrderViewHolder(@NonNull View itemView) {
super(itemView);
txtOrderName = itemView.findViewById(R.id.order_name);
txtOrderHp = itemView.findViewById(R.id.order_contactNumber);
txtOrderAddress = itemView.findViewById(R.id.order_address);
txtOrderDate = itemView.findViewById(R.id.order_date);
txtOrderServices = itemView.findViewById(R.id.order_services);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View view ) {
itemClickListener.onClick(view,getAdapterPosition(), false);
}
}