gpt4 book ai didi

java - 从 Firebase 在 Android 中获取数据时出错

转载 作者:行者123 更新时间:2023-11-29 23:58:57 25 4
gpt4 key购买 nike

这是我最近两天遇到的错误。

请给我解决方案

Process: com.example.umangthakkar.electrofycustomer, PID: 25756 com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.umangthakkar.electrofycustomer.BillPaid

Process: com.example.umangthakkar.electrofycustomer, PID: 25756
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.umangthakkar.electrofycustomer.BillPaid at com.google.android.gms.internal.zg.zzb(Unknown Source)
at com.google.android.gms.internal.zg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.example.umangthakkar.electrofycustomer.RecyclerView_MyBills$1.onDataChange(RecyclerView_MyBills.java:75)
at com.google.android.gms.internal.to.zza(Unknown Source)
at com.google.android.gms.internal.vj.zzHX(Unknown Source)
at com.google.android.gms.internal.vp.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5740)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)

下面是写的代码请看懂

package com.example.umangthakkar.electrofycustomer;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class RecyclerView_MyBills extends AppCompatActivity {

DatabaseReference dbBillPaid;

ListView rvCustomers;

//List<Customer> customerList;
List<BillPaid> billPaidList;

private ProgressBar progressBar;
private TextView txtplwait;

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

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();
dbBillPaid = FirebaseDatabase.getInstance().getReference("BillPaid").child(userid);
rvCustomers = (ListView) findViewById(R.id.rvCustomers);
// rvCustomers.setHasFixedSize(true);

//customerList = new ArrayList<>();
billPaidList = new ArrayList<>();



progressBar = (ProgressBar) findViewById(R.id.progressBar);
txtplwait = (TextView) findViewById(R.id.txtpwait);

progressBar.setVisibility(View.VISIBLE);
txtplwait.setVisibility(View.VISIBLE);

}

@Override
protected void onStart() {
super.onStart();

dbBillPaid.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

billPaidList.clear();


for(DataSnapshot customerSnapshot : dataSnapshot.getChildren()){
// Customer customer = customerSnapshot.getValue(Customer.class);
BillPaid billPaid = customerSnapshot.getValue(BillPaid.class);

billPaidList.add(billPaid);
}

MyBillList adapter = new MyBillList(RecyclerView_MyBills.this,billPaidList);
rvCustomers.setAdapter(adapter);

progressBar.setVisibility(View.GONE);

txtplwait.setVisibility(View.GONE);

if (dataSnapshot.getValue()==null)
{

showBillsDailog();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

private void showBillsDailog() {

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.nomybills, null);
dialogBuilder.setView(dialogView);


final Button buttonyes = (Button) dialogView.findViewById(R.id.btnyes);
// final Button buttonno = (Button) dialogView.findViewById(R.id.btnno);

dialogBuilder.setTitle("No Bills");
final AlertDialog b = dialogBuilder.create();
b.show();


buttonyes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RecyclerView_MyBills.this, Drawer_MainPage.class);
startActivity(intent);
finish();
b.dismiss();
}
});


}
}

我的账单列表.java :

package com.example.umangthakkar.electrofycustomer;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
* Created by Umang Thakkar on 14/04/2018.
*/

public class MyBillList extends ArrayAdapter<BillPaid> {

private Activity context;
private List<BillPaid> billPaidList;

public MyBillList(Activity context, List<BillPaid> billPaidList)
{
super(context,R.layout.activity_card_view_mybills,billPaidList);
this.context=context;
this.billPaidList=billPaidList;
// this.customerList=customerList;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();

View ListViewItem = inflater.inflate(R.layout.activity_card_view_mybills,null,true);

// TextView firstname= (TextView) ListViewItem.findViewById(R.id.fname);
// TextView lastname= (TextView) ListViewItem.findViewById(R.id.lname);
// TextView service= (TextView) ListViewItem.findViewById(R.id.service_no);
TextView month= (TextView) ListViewItem.findViewById(R.id.month);
TextView year= (TextView) ListViewItem.findViewById(R.id.year);
TextView gross= (TextView) ListViewItem.findViewById(R.id.gross);

// Customer customer = customerList.get(position);

BillPaid billPaid = billPaidList.get(position);

// firstname.setText(customer.getFname());
//lastname.setText(customer.getLname());
//service.setText(customer.getService());
month.setText(billPaid.getBill_Month());
year.setText(billPaid.getBill_Year());
gross.setText(billPaid.getPaid_Amount());

return ListViewItem;
}
}

BIllPaid.java :

package com.example.umangthakkar.electrofycustomer;

/**
* Created by Umang Thakkar on 27/04/2018.
*/
public class BillPaid {
private String Order_Id;
private String Transaction_Id;
private String Bill_Id;
private String Bill_Date;
private String Paid_Date;
private String Paid_Amount;
private String Bill_Month;
private String Bill_Year;

public BillPaid()
{

}

public BillPaid(String order_Id, String transaction_Id, String bill_Id, String bill_Date, String paid_Date, String paid_Amount, String bill_Month, String bill_Year) {
Order_Id = order_Id;
Transaction_Id = transaction_Id;
Bill_Id = bill_Id;
Bill_Date = bill_Date;
Paid_Date = paid_Date;
Paid_Amount = paid_Amount;
Bill_Month = bill_Month;
Bill_Year = bill_Year;
}

public String getOrder_Id() {
return Order_Id;
}

public void setOrder_Id(String order_Id) {
Order_Id = order_Id;
}

public String getTransaction_Id() {
return Transaction_Id;
}

public void setTransaction_Id(String transaction_Id) {
Transaction_Id = transaction_Id;
}

public String getBill_Id() {
return Bill_Id;
}

public void setBill_Id(String bill_Id) {
Bill_Id = bill_Id;
}

public String getBill_Date() {
return Bill_Date;
}

public void setBill_Date(String bill_Date) {
Bill_Date = bill_Date;
}

public String getPaid_Date() {
return Paid_Date;
}

public void setPaid_Date(String paid_Date) {
Paid_Date = paid_Date;
}

public String getPaid_Amount() {
return Paid_Amount;
}

public void setPaid_Amount(String paid_Amount) {
Paid_Amount = paid_Amount;
}

public String getBill_Month() {
return Bill_Month;
}

public void setBill_Month(String bill_Month) {
Bill_Month = bill_Month;
}

public String getBill_Year() {
return Bill_Year;
}

public void setBill_Year(String bill_Year) {
Bill_Year = bill_Year;
}
}

This is the error m getting in detail

最佳答案

在你的代码中,你有这个:

dbBillPaid.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

billPaidList.clear();


for(DataSnapshot customerSnapshot : dataSnapshot.getChildren()){
// Customer customer = customerSnapshot.getValue(Customer.class);
BillPaid billPaid = customerSnapshot.getValue(BillPaid.class);

billPaidList.add(billPaid);
}

上面的 for 循环在直接子级内部迭代并返回一个字符串,因此您会得到该错误。

假设你有这个数据库:

billpaid
pushid
order_id:...

然后删除循环并使用以下解决错误:

dbBillPaid.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

billPaidList.clear();

BillPaid billPaid = dataSnapshot.getValue(BillPaid.class);

billPaidList.add(billPaid);
}

关于java - 从 Firebase 在 Android 中获取数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50142895/

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