1

This is the error that I have been facing for the last two days.

Kindly give me solution for the same

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)

Below is the code written please check to understand

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();
        }
    });


}
}

MyBillList.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

1 Answer 1

1

In your code, you have this:

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);
        }

The for loop above is iterating inside the direct children and returning a String, thus you get that error.

Assuming you have this db:

billpaid
     pushid
        order_id:...

Then remove the the loop and use the following to solve the error:

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

        billPaidList.clear();

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

            billPaidList.add(billPaid);
        }
Sign up to request clarification or add additional context in comments.

4 Comments

Thankyou Sir Your assumption for Database is correct. But if i dont use For loop than will if get multiple records??
I tried removing for loop and there is no such error now. but as i said it fetches only 1 Order or Row. I want each row to display i.e. more than 1 row. Than is there any alternate of FOR Loop?? I also tried Type Casting when i was using For loop.... BillPaid billPaid = (BillPaid) dataSnapshot.getValue(BillPaid.class); but still not resolved
Thankyou Sir. It is been resolved. No For Loop required. I made a mistake in creating DB as u assumed was my goal but forgot to make entry of pushid.
okay great, also if you want to retrieve each field alone then just do, with a for loop: String name=customerSnapshot.child("order_id").getValue().toString()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.