FirebaseRecycler不会在DatabaseReference中检索带有字符串的列表

时间:2019-06-13 23:14:52

标签: android firebase

接下来我将显示的代码一直以来都在使用,现在出现此错误:

drawsAsynchronously

com.aplicacion.vivaluganoapp.ar.Model.UserPedido是一个Java类

代码:

   com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.aplicacion.vivaluganoapp.ar.Model.UserPedido

错误位于此处:

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

        currentUserID = Prevalent.currentOnlineUser.getTelefono();

        ordersList =  findViewById(R.id.pedidorecycler);
        ordersList.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    protected void onStart() {
        super.onStart();
        ordersRef = FirebaseDatabase.getInstance().getReference().child("Usuarios").child(currentUserID).child("Pedidos");
        FirebaseRecyclerOptions<UserPedido> options =
                new FirebaseRecyclerOptions.Builder<UserPedido>()
                .setQuery(ordersRef, UserPedido.class)
                .build();

        FirebaseRecyclerAdapter<UserPedido, UserProductViewHolder> adapter =
                new FirebaseRecyclerAdapter<UserPedido, UserProductViewHolder>(options) {
                    @Override
                    protected void onBindViewHolder(@NonNull UserProductViewHolder holder, final int position, @NonNull UserPedido model) {
                        holder.userName.setText("Nombre: " + model.getNombre());
                        holder.userPhoneNumber.setText("Numero de Telefono: " + model.getTelefono());
                        holder.userTotalPrice.setText("Monto Total: " + model.getMontoTotal() + " ARS");
                        holder.userDateTime.setText("Orden: " + model.getTiempo() + "    " + model.getHora());
                        holder.userShippingAddress.setText("Direccion de Envio o Retiro: " + model.getDireccion() + ", " + model.getCiudad());
                        holder.userBirthday.setText("Cumpleaños:" + model.getCumpleaños());
                        holder.userInfoExtra.setText("Informacion Extra: " + model.getInformacionExtra());


                        holder.ShowOrdersBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                String uID = getRef(position).getKey();

                                Intent intent = new Intent(comprobarPedidoActivity.this, adminUserProductsActivity.class);
                                intent.putExtra("uid", uID);
                                startActivity(intent);
                            }
                        });

                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                CharSequence options[] = new CharSequence[]
                                        {
                                                "Si",
                                                "No"
                                        };
                                AlertDialog.Builder builder = new AlertDialog.Builder(comprobarPedidoActivity.this);
                                builder.setTitle("Pedido");

                                builder.setItems(options, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int i) {

                                        if(i == 0)
                                        {
                                            String uID = getRef(position).getKey();

                                            RemoverPedido(uID);

                                        }
                                        if(i == 1)
                                        {

                                            finish();
                                        }

                                    }
                                });

                                builder.show();
                            }
                        });
                    }

                    @NonNull
                    @Override
                    public UserProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.pedidos_layout, viewGroup, false);
                        return new UserProductViewHolder(view);
                    }
                };
        ordersList.setAdapter(adapter);
        adapter.startListening();
    }
    public static class UserProductViewHolder extends RecyclerView.ViewHolder
    {
        public TextView userName, userPhoneNumber, userTotalPrice, userDateTime, userShippingAddress, userBirthday, userInfoExtra;
        public Button ShowOrdersBtn;

        public UserProductViewHolder(View itemView){
            super(itemView);

            userName =  itemView.findViewById(R.id.pedido_nombre_usuario);
            userPhoneNumber =  itemView.findViewById(R.id.pedido_telefono_usuario);
            userTotalPrice =  itemView.findViewById(R.id.pedido_precio_total);
            userDateTime =  itemView.findViewById(R.id.pedido_tiempo_hora);
            userShippingAddress =  itemView.findViewById(R.id.pedido_direccion_city);
            ShowOrdersBtn =  itemView.findViewById(R.id.mostrar_todos_productos_btn);
            userBirthday = itemView.findViewById(R.id.pedido_cumpleaños);
            userInfoExtra = itemView.findViewById(R.id.pedido_info_extra);


        }
    }

每次我将字符串currentUserID(这是其用户ID的引用在数据库中)放入时,都会看到上述错误。 这很奇怪,因为我总是使用相同的代码来执行该操作,并且除了添加currentUserID(我在其他Java类和  工作完美)。 为什么该代码现在对我不起作用,如何解决?

1 个答案:

答案 0 :(得分:0)

由于您尝试使用UserPedido类来获取数据,因此您需要将引用更改为

ordersRef = FirebaseDatabase.getInstance().getReference().child("Usuarios").child(currentUserID)

如果这不能解决问题。请分享您的数据库结构