无法将 java.lang.String 类型的对象转换为 Orders_Class 类型。我该如何修复它?

问题描述 投票:0回答:1

如何从订单子项中的产品检索数据Firebase 实时数据库

这是我的 Orders_class

该类仅包含 orderId 和 Order full pay

package com.example.safetysouq.Orders_Classes;

public class Orders_Class {
    String OrderId,FullPay,ProductName;

    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String productName) {
        ProductName = productName;
    }

    public String getFullPay() {
        return FullPay;
    }

    public void setFullPay(String fullPay) {
        FullPay = fullPay;
    }

    public String getOrderId() {
        return OrderId;
    }

    public void setOrderId(String orderId) {
        OrderId = orderId;
    }
}

这是我的 Orders_adapter

该类仅包含 orderId 和 Order full pay

public class Orders_Adapter extends BaseAdapter {
    ArrayList<Order_Item_Class>orderItemClassArrayList;
    Orders_Item_Adapter ordersItemAdapter;
    
    Context context;
    DatabaseReference databaseReference,databaseReference2;
    FirebaseAuth firebaseAuth;
    ArrayList<Orders_Class>ordersClassArrayList;
    ArrayList<SingleCartProductInformationDataClass>singleCartProductInformationDataClassArrayList;
    LayoutInflater layoutInflater;


    public Orders_Adapter(Context context, DatabaseReference databaseReference, FirebaseAuth firebaseAuth, ArrayList<Orders_Class> ordersClassArrayList) {
        this.context = context;
        this.databaseReference = databaseReference;
        this.firebaseAuth = firebaseAuth;
        this.ordersClassArrayList = ordersClassArrayList;
    }

    @Override
    public int getCount() {
        return ordersClassArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (layoutInflater==null){
            layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if(convertView==null){
            convertView =layoutInflater.inflate(R.layout.orders_gridview,null);
        }
        TextView orders_id =convertView.findViewById(R.id.orderid);
        TextView order_total_price = convertView.findViewById(R.id.ordertotalprice);
        GridView order_item_grid_view = convertView.findViewById(R.id.ordersitemsgridview);

        orders_id.setText("Order ID : "+"#"+ordersClassArrayList.get(position).getOrderId());
        order_total_price.setText("Total Money : "+ordersClassArrayList.get(position).getFullPay()+" EGP");

        return convertView;
    }
}

这是我的订单页面(活动)

package com.example.safetysouq.Orders_Classes;

import android.os.Bundle;
import android.widget.GridView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.safetysouq.R;
import com.google.firebase.auth.FirebaseAuth;
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;

public class Orders_Page extends AppCompatActivity {
    private GridView ordersGridview;
    private ArrayList<Orders_Class> ordersClassArrayList;
    private Orders_Adapter ordersAdapter;
    private DatabaseReference databaseReference;
    private FirebaseAuth firebaseAuth;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders_page);
        ordersGridview=findViewById(R.id.ordersGridview);
        databaseReference = FirebaseDatabase.getInstance().getReference();
        firebaseAuth= FirebaseAuth.getInstance();

        databaseReference=FirebaseDatabase.getInstance().getReference().child("Orders").child(firebaseAuth.getCurrentUser().getUid());
        ordersClassArrayList=new ArrayList<>();
        ordersAdapter = new Orders_Adapter(Orders_Page.this,databaseReference,firebaseAuth,ordersClassArrayList);
        ordersGridview.setAdapter(ordersAdapter);
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for(DataSnapshot dataSnapshot:snapshot.getChildren()){
                    Orders_Class ordersClass = dataSnapshot.getValue(Orders_Class.class);
                    ordersClassArrayList.add(ordersClass);
                }
                ordersAdapter.notifyDataSetChanged();
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }
}
java android firebase firebase-realtime-database
1个回答
0
投票

您收到以下错误:

无法将 java.lang.String 类型的对象转换为 Orders_Class 类型

因为您创建了一个指向:

db/Orders/$uid

当您循环遍历结果并尝试将子级转换为

Orders_Class
类型的对象时,这确实是不可能的,因为在该节点内部,只有由 UID 表示的字符串,而不是
Orders_Class
对象,因此出现错误。

要获取

orders
节点下存在的所有用户的所有订单,您必须创建一个指向
orders
节点的引用,而不是指向对应于单个用户的节点,然后监听真实的 -时间更新,如以下代码行所示:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference ordersRef = db.child("Orders");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot uidSnapshot : dataSnapshot.getChildren()) {
            for(DataSnapshot orderSnapshot : uidSnapshot.getChildren()) {
                Orders_Class order = orderSnapshot.getValue(Orders_Class.class);
                Log.d("TAG", order.toString());
            }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Log.d("TAG", error.getMessage()); //Never ignore potential errors!
    }
};

如您所见,您必须循环两次才能从数据库中获取订单对象。

但是,只有当类内的字段名称与数据库中的字段名称匹配时,此解决方案才有效。因此,有关更多信息,请查看我在以下帖子中的回答:

如果您考虑在某个时间点尝试使用 Cloud Firestore,我建议您使用以下架构:

db
|
--- users (collection)
     |
     --- $uid (document)
          |
          --- order (sub-collection)
               |
               --- $orderId (document)
                    |
                    --- //Order fields

并使用集合组查询,以更简单优雅的方式获取所有用户的所有订单。

© www.soinside.com 2019 - 2024. All rights reserved.