How to get user order data in Firebase database?

Asked

Viewed 117 times

1

I have an Android app in Kotlin that orders pizza delivery where the company receives the data of the user’s order and I want the user to also receive the data of the order he made, some time ago I try and do not find the solution. Following method should recover order data in order view class:

   private fun recuperarDados() {

        dialog = SpotsDialog.Builder()
            .setContext(this)
            .setMessage("Aguarde")
            .setCancelable(false)
            .build()
        dialog!!.show()



        val pedidoRef = idEmpresa?.let {
            idUsuarioLogado?.let { it1 ->
                firebaseRef
                    ?.child("pedidos_usuario")
                    ?.child("itens")

            }
        }

        val pedidoPesquisa = pedidoRef?.child("status")?.equalTo("confirmado")

        pedidoPesquisa?.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {

                pedidos.clear()
                if (dataSnapshot.value != null) {
                    for (ds in dataSnapshot.children) {
                        val pedido = ds.getValue(ItemPedido::class.java)
                        itemPedidos.add(pedido!!)

                    }
                    adapterPedido!!.notifyDataSetChanged()
                    dialog!!.dismiss()

                }




            }

            override fun onCancelled(databaseError: DatabaseError) {

            }
        })


    }
  • Good morning! So are two different apps? One for the user and one for the company?

  • that’s right, there are two

1 answer

2

I would make the base structure this way

inserir a descrição da imagem aqui

To filter the orders by the company side

val pedidoPesquisa = mFirebaseDatabaseReference.child("pedidos")
            .orderByChild("idempresa")
            .equalTo(1);//seu id da empresa

To filter user side requests

   val pedidoPesquisa =  mFirebaseDatabaseReference.child("pedidos")
            .orderByChild("idusuario")
            .equalTo(1);//seu id de usuario

To execute the search

   pedidoPesquisa?.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {

            if (dataSnapshot.value != null) {
                for (ds in dataSnapshot.children) {
                    val pedido = ds.getValue(ItemPedido::class.java)
                    itemPedidos.add(pedido!!)

                }
                adapterPedido!!.notifyDataSetChanged()

            }

        }

        override fun onCancelled(databaseError: DatabaseError) {

        }
    })
  • How recycleview can receive this data?

  • I tried in recycleview but returns null

  • a question, do you want to list all orders with "confirmed" status? or you want to pick up items from an order?

  • With status "confirmed"

  • once confirmed the order goes to a finalized status?

  • I messed up the explanation, how do I remake my explanation?

  • Firebase is more work of how to assemble the structure and what you will consult, I think you want to list the confirmed orders for the company and the same that you did as a user, I would separate the ones that are in progress and you are waiting and the others that finished even if you have to copy the data from one list to another when you finish, I don’t know if you understand me but I can try to put together a structure for you and you see if it suits your project

  • I would appreciate it, I just need to bring the orders made by the client to see what you asked

  • Until then, I just can’t recover the node items that are on the requested node, in the other hand I can return other nodes like the list of companies, for example

  • Does it make sense that by your code this happens, within your node items have the account id? if it has not included, and for each your request make an id similar to the one I answered there, why ai vc can filter using child("pedidos_usuario") . orderByChild("idclient") . equalTo(1);

  • Managed to understand the explanation Firebase is sometimes confusing even to search for things is different for example from an SQL database I have difficulty to build things there too

  • I managed to solve otherwise, with the help of a friend

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.