gpt4 book ai didi

java - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.String 输入 com.example.myapplication.Orders

转载 作者:行者123 更新时间:2023-12-01 16:39:12 24 4
gpt4 key购买 nike

我已经检查了其他问题。他们都不能解决我的问题。我想从 Firebase 实时数据库的节点 “orderDetails” 检索数据并在 RecyclerView 中显示它们

UserActivity.java

private DatabaseReference databaseReference;
RecyclerView ordersList;
FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);

firebaseAuth = FirebaseAuth.getInstance();
firebaseUser = firebaseAuth.getCurrentUser();

String pickerUID = FirebaseAuth.getInstance().getCurrentUser().getUid();
try {
DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference = mDatabaseReference.child("Pickers").child(pickerUID).child("orderDetails");
databaseReference.keepSynced(true);

ordersList = findViewById(R.id.recycler);
ordersList.setHasFixedSize(false);
ordersList.setLayoutManager(new LinearLayoutManager(this));
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Orders,OrdersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Orders, OrdersViewHolder>
(Orders.class, R.layout.orders_list, OrdersViewHolder.class, databaseReference) {
@Override
protected void populateViewHolder(OrdersViewHolder ordersViewHolder, Orders orders, int i) {

try {
ordersViewHolder.setUserName(orders.getUserName());
ordersViewHolder.setUserAddress(orders.getUserAddress());
ordersViewHolder.setUserEmail(orders.getUserEmail());
ordersViewHolder.setDate(orders.getDate());
ordersViewHolder.setTime(orders.getTime());
ordersViewHolder.setUserUID(orders.getUserUID());
ordersViewHolder.setUserPhoneNumber(orders.getPhoneNumber());
} catch (Exception e) {
Toast.makeText(UserActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
};
ordersList.setAdapter(firebaseRecyclerAdapter);
}

public static class OrdersViewHolder extends RecyclerView.ViewHolder {

View view;
public TextView userUIDTV;

public OrdersViewHolder(View itemView) {
super(itemView);
view=itemView;
}

public void setUserName(String userName) {
TextView name = view.findViewById(R.id.userName);
name.setText(userName);
}
public void setUserAddress(String userAddress) {
TextView address = view.findViewById(R.id.userAddress);
address.setText(userAddress);
}
public void setUserEmail(String userEmail) {
TextView email = view.findViewById(R.id.userEmail);
email.setText(userEmail);
}
public void setDate(String date) {
TextView userDate = view.findViewById(R.id.date);
userDate.setText(date);
}
public void setTime(String time) {
TextView userTime = view.findViewById(R.id.time);
userTime.setText(time);
}
public void setUserUID(String userUID) {
userUIDTV = view.findViewById(R.id.userUID);
userUIDTV.setText(userUID);

}
public void setUserPhoneNumber(String userPhoneNumber) {
TextView phoneNumber = view.findViewById(R.id.userPhoneNumber);
phoneNumber.setText(userPhoneNumber);
}

}
}

Orders.java

public class Orders {

private String userName;
private String userAddress;
private String userEmail;
private String date;
private String time;
private String userUID;
private String phoneNumber;

public Orders(String userName, String userAddress, String userEmail, String date, String time, String phoneNumber, String userUID) {
this.userName = userName;
this.userAddress = userAddress;
this.userEmail = userEmail;
this.date = date;
this.time = time;
this.phoneNumber = phoneNumber;
this.userUID = userUID;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserAddress() {
return userAddress;
}

public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}

public String getUserEmail() {
return userEmail;
}

public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setUserPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getUserUID() {
return userUID;
}

public void setUserUID(String userUID) {
this.userUID = userUID;
}

public Orders() {

}
}

我的数据库 https://ibb.co/DCHfBDt

我想从节点“orderDetails”获取数据

我该怎么做?

最佳答案

更改数据库结构并在 orderDetails 下添加随机 ID:

orderDetails
randomId
date : date_value
phoneNumber : phoneNo_value

现在,当你这样做时:

databaseReference = mDatabaseReference.child("Pickers").child(pickerUID).child("orderDetails");

它将检索Orders类型的数据

关于java - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.String 输入 com.example.myapplication.Orders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61898677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com