我正在使用firebaseui填充recyclerview。从firebase提取数据时遇到问题。
databaseReference.child("BatchPurchased").child("TotalCount").runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
payId = "";
Long value = mutableData.getValue(Long.class);
if (mutableData.getValue() == null) {
payId = "1"+"_tx";
mutableData.setValue(1);
} else {
Long countset = (value + 1);
payId = String.valueOf(countset.toString())+"_tx";
mutableData.setValue(value + 1);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean success, DataSnapshot dataSnapshot) {
if (databaseError != null) {
System.out.println("Firebase counter increment failed.");
} else {
System.out.println("Firebase counter increment succeeded.");
DatabaseReference databaseReference1 = firebaseDatabase.getReference("User").child("BatchPurchased");
BatchPurchased batchPurchased = new BatchPurchased(post.getUserId().toString(), userLogin.getUid().toString(), "50", post.getDecryptKey().toString(), "done", dateMy().toString(), "google_pay"); databaseReference1.child(payId).setValue(batchPurchased);
}
}
});
}]
public class BatchPurchased {
String owner_ID;
String sender_ID;
String price;
String token_ID;
String purchased_status;
String purchased_date;
String payment_mode;
public BatchPurchased(String owner_ID, String sender_ID, String price, String token_ID, String purchased_status, String purchased_date, String payment_mode) {
this.owner_ID = owner_ID;
this.sender_ID = sender_ID;
this.price = price;
this.token_ID = token_ID;
this.purchased_status = purchased_status;
this.purchased_date = purchased_date;
this.payment_mode = payment_mode;
}
public BatchPurchased() {
}
public String getOwner_ID() {
return owner_ID;
}
public void setOwner_ID(String owner_ID) {
this.owner_ID = owner_ID;
}
public String getSender_ID() {
return sender_ID;
}
public void setSender_ID(String sender_ID) {
this.sender_ID = sender_ID;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getToken_ID() {
return token_ID;
}
public void setToken_ID(String token_ID) {
this.token_ID = token_ID;
}
public String getPurchased_status() {
return purchased_status;
}
public void setPurchased_status(String purchased_status) {
this.purchased_status = purchased_status;
}
public String getPurchased_date() {
return purchased_date;
}
public void setPurchased_date(String purchased_date) {
this.purchased_date = purchased_date;
}
public String getPayment_mode() {
return payment_mode;
}
public void setPayment_mode(String payment_mode) {
this.payment_mode = payment_mode;
}
}
public void dataget(){
try {
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("User").child("BatchPurchased").;
FirebaseRecyclerOptions<BatchPurchased> options =
new FirebaseRecyclerOptions.Builder<BatchPurchased>()
.setQuery(query,BatchPurchased.class)
.build();
adapter = new FirebaseRecyclerAdapter<BatchPurchased, AllOrder>(options) {
@NonNull
@Override
public AllOrder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.all_order_items, parent, false);
return new AllOrder(view);
}
@Override
protected void onBindViewHolder(AllOrder holder, int position, final BatchPurchased model) {
holder.bindToPost(model, MyOrdersActivity.this);
BatchPurchased category = model;
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
}
});
}
};
recyclerview.setAdapter(adapter);
}catch (Exception e){
e.printStackTrace();
}
}
例外:
com.google.firebase.database.DatabaseException:无法将类型为java.lang.Long的对象转换为com.example.admin.omnicam.FcmModel.BatchPurchased
从firebase提取数据时遇到的问题。请帮助解决此异常。
答案 0 :(得分:0)
问题出在您的数据结构中。
在这段代码中,您尝试将每个子快照解析为BatchPurchased数据模型。
FirebaseRecyclerOptions<BatchPurchased> options =
new FirebaseRecyclerOptions.Builder<BatchPurchased>()
.setQuery(query,BatchPurchased.class)
.build();
但是,不能将名为“ TotalCount”的子项解析为BatchPurchased。 考虑重组您在Firebase中存储数据的方式。