错误"无法将java.lang.String类型的对象转换为"

时间:2017-11-04 18:26:58

标签: java android firebase-realtime-database

大家好我已经彻底研究了这个问题,我看到有关于这个问题的堆栈上有几个主题,但没有答案。如果有人能给我一个解决方案,我会非常感激。这是我的代码:

public class Deliveries extends Fragment {
    DatabaseReference dbRef;
    FirebaseDatabase myRef;
    List<customerInfo>myList;
    View delView;
    RecyclerView custCards;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        delView = inflater.inflate(R.layout.activity_delivery_card_layout,container,false);
        custCards = (RecyclerView)delView.findViewById(R.id.recyclerViewResults);
        myRef = FirebaseDatabase.getInstance();
        dbRef = myRef.getReference("customers123456");
        custCards.setHasFixedSize(true);
        custCards.setLayoutManager(new LinearLayoutManager(delView.getContext()));

        dbRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                myList = new ArrayList<customerInfo>();
                for(DataSnapshot snapshot:dataSnapshot.getChildren()){
                    customerInfo val = snapshot.getValue(customerInfo.class);
                    customerInfo customerInfo = new customerInfo();

                    String nme,id,delType,km;
                    nme = val.getCustName();
                    id = val.getCustID();
                    delType = val.getDeliveryType();
                    km = val.getKmTravel();

                    customerInfo.setCustName(nme);
                    customerInfo.setCustID(id);
                    customerInfo.setDeliveryType(delType);
                    customerInfo.setKmTravel(km);

                    myList.add(customerInfo);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        RecyclerAdapter recyclerAdapter = new RecyclerAdapter(myList,delView.getContext());
        custCards.setItemAnimator(new DefaultItemAnimator());
        custCards.setAdapter(recyclerAdapter);
        return delView;
    }
}

从firebase提取的数据需要填充cardview。这是我的循环视图适配器类:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Viewholder> {
    Context context;
    List<customerInfo> info;

    public RecyclerAdapter(List<customerInfo> temp,Context _context){
        this.context = _context;
        info = temp;
    }

    @Override
    public RecyclerAdapter.Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View myView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_delivery,parent,false);
        Viewholder viewholder = new Viewholder(myView);
        return viewholder;
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.Viewholder holder, int position) {
        customerInfo myLst = info.get(position);
        holder.cName.setText(myLst.getCustName());
        holder.cID.setText(myLst.getCustID());
        holder.cDel.setText(myLst.getDeliveryType());
        holder.cKm.setText(myLst.getKmTravel());
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public class Viewholder  extends RecyclerView.ViewHolder{
        public TextView cName;
        public TextView cID;
        public TextView cDel;
        public TextView cKm;

        public Viewholder(View item){
            super(item);
            cName = (TextView)item.findViewById(R.id.txtvwCustNme);
            cID = (TextView)item.findViewById(R.id.txtvwCustID);
            cDel = (TextView)item.findViewById(R.id.txtvwDelType);
            cKm = (TextView)item.findViewById(R.id.txtvwEstKm);
        }
    }
}

这是我的客户信息类:

public class customerInfo {
    public String custName;
    public String custID;
    public String deliveryType;
    public String kmTravel;


    public String getCustID() {
        return custID;
    }

    public void setCustID(String custID) {
        this.custID = custID;
    }

    public String getKmTravel() {
        return kmTravel;
    }


    public String getCustName() {
        return custName;
    }

    public String getDeliveryType() {
        return deliveryType;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public void setDeliveryType(String deliveryType) {
        this.deliveryType = deliveryType;
    }

    public void setKmTravel(String kmTravel) {
        this.kmTravel = kmTravel;
    }
}

就像我之前所说的那样,我一直试图通过查看已经发布在堆栈上但之前没有运气的先前问题来解决这个问题。我有另一个类,它从firebase中拉出来并且它完美地运行,两个类之间的区别是:

交付类(不工作):

customerInfo val = snapshot.getValue(customerInfo.class);

家庭班(作品):

myData.add(snapshot.getValue(String.class));

来自android监视器的确切错误:

FATAL EXCEPTION: main
                                                                           Process: com.example.anthe.question2, PID: 4947
                                                                           com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.anthe.question2.customerInfo
                                                                               at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
                                                                               at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                                               at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                               at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                               at com.example.anthe.question2.Deliveries$1.onDataChange(Deliveries.java:44)
                                                                               at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                                                                               at com.google.android.gms.internal.zzbqx.zzZV(Unknown Source)
                                                                               at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

数据库结构: db structure

1 个答案:

答案 0 :(得分:0)

我不知道代码的结构,但我认为myRef.getReference("customers123456")已经获得了对特定用户的引用。

如果您使用for(DataSnapshot snapshot:dataSnapshot.getChildren())遍历所有孩子,snapshot将是您的customerInfo类的属性/元素(例如:custName - 代表字符串)

因此,您应该删除循环,或者引用此“customers123456”节点的父节点以获取您的客户列表。