0

I have a database structure like this (sample):

ID_EMPRESA
-name:
-adress:
-status: Aberto
-timestamp: 0154254521
-status_timestamp: Aberto_0154254521

I need to populate my RecyclerView with data from a Firebase reference

Since it is not possible to work with multiple queries when querying Firebase data, according to the structure of the database I tried the following filter:

mDatabase.child(ID_EMPRESA).orderByChild("status_timeStamp").startAt("Open").endAt("Open\uf8ff")

So I retrieve the data that has status: Open

Code RecyclerDapter:

FirebaseRecyclerAdapter<CardPedidos_row, CardPedidosViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<CardPedidos_row, CardPedidosViewHolder>(


                CardPedidos_row.class,
                R.layout.card_agendamentos_row,
                CardPedidosViewHolder.class,
                mDatabase.child(ID_EMPRESA).orderByChild("status_timeStamp").startAt("Open").endAt("Open\uf8ff")

)

How could I recover the data in descending order, since it already has the timestamp stored. As it is, it is bringing Ascending

FullCode - Fragment:

public class PedidosTab1 extends Fragment {


    private RecyclerView mCardPedidos;
    private DatabaseReference mDatabaseEmpresa;
    private DatabaseReference mDatabaseAgendamentos;

    private FirebaseAuth mAuth;
    private FirebaseUser mCurrentUser;

    private TextView mTextPadrao;

    private Query query;

    public PedidosTab1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_pedidos_tab1, container, false);

        /*Recuperando instancia do Firebase*/
        mAuth = FirebaseAuth.getInstance();
        mCurrentUser = mAuth.getCurrentUser();

        mDatabaseEmpresa = FirebaseDatabase.getInstance().getReference().child("Empresas").child(mCurrentUser.getUid());
        mDatabaseAgendamentos = FirebaseDatabase.getInstance().getReference().child("Vendas_Empresas");


        /*Atributos tela*/
        mTextPadrao = (TextView) view.findViewById(R.id.tvPedTab1_textPadrao);


        /*RecyclerView*/
        mCardPedidos = (RecyclerView) view.findViewById(R.id.cardListaPedidos);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);

        mCardPedidos.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        carregarPedidos();


        return view;
    }

    private void carregarPedidos() {

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

                if (dataSnapshot.child(mCurrentUser.getUid()).exists()){

                    carregarDadosRecycler();

                } else {

                    mTextPadrao.setVisibility(View.VISIBLE);
                    mCardPedidos.setVisibility(View.GONE);

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    private void carregarDadosRecycler() {

        FirebaseRecyclerAdapter<CardPedidos_row, CardPedidosViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<CardPedidos_row, CardPedidosViewHolder>(


                CardPedidos_row.class,
                R.layout.card_agendamentos_row,
                CardPedidosViewHolder.class,
                mDatabaseAgendamentos.child(mCurrentUser.getUid()).orderByChild("status_timeStamp").startAt("Aberto").endAt("Aberto\uf8ff")


        ) {
            @Override
            protected void populateViewHolder(final CardPedidosViewHolder viewHolder, final CardPedidos_row model, int position) {

                final String pedido_key = getRef(position).getKey();
                String nome_servico = model.getEmpresa_nome();
                String nome_empresa = model.getEmpresa_nome();
                final String id_empresa = model.getEmpresa_id();
                String valor_servico = model.getServico_valor();
                String hora = model.getAgenda_hora();
                String data = model.getAgenda_data();
                String status = model.getStatus_situacao();
                String timeStamp = model.getTimestamp_criacaoDt();



                viewHolder.setServico_nome(model.getServico_nome());
                viewHolder.setAgenda_data(model.getAgenda_data());
                viewHolder.setAgenda_hora(model.getAgenda_hora());
                viewHolder.setServico_valor(model.getServico_valor());
                viewHolder.setEmpresa_nome(model.getEmpresa_nome());
                viewHolder.setStatus_situacao(model.getStatus_situacao());
                viewHolder.setTimestamp_criacaoDt(model.getTimestamp_criacaoDt());

                /*Clique na View*/
                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        /*Intent detalhePedido = new Intent(getActivity().getApplication(), DetalhePedido.class);
                        detalhePedido.putExtra("id_empresa", id_empresa);
                        detalhePedido.putExtra("id_venda", pedido_key);
                        startActivity(detalhePedido);*/

                        Intent detalhePedido = new Intent(getActivity().getApplication(), DetalhePedido.class);
                        detalhePedido.putExtra("id_pedido", pedido_key);
                        startActivity(detalhePedido);



                    }
                });



            }
        };

        mCardPedidos.setAdapter(firebaseRecyclerAdapter);



    }

    public static class CardPedidosViewHolder extends RecyclerView.ViewHolder{

        View mView;

        public CardPedidosViewHolder ( View itemView ){

            super(itemView);

            mView = itemView;

        }

        public void setServico_nome(String servico_nome){

            TextView cardNome_servico = (TextView) mView.findViewById(R.id.tvNomeServico_CardAg);
            cardNome_servico.setText(servico_nome);
        }

        public void setEmpresa_nome(String empresa_nome){

            TextView cardNome_empresa = (TextView) mView.findViewById(R.id.tvNomeEmpresa_CardAg);
            cardNome_empresa.setText(empresa_nome);
        }

        public void setServico_valor(String servico_valor){

            TextView cardValor_servico = (TextView) mView.findViewById(R.id.tvValor_CardAg);
            cardValor_servico.setText(servico_valor);

        }

        public void setAgenda_data(String agenda_data){

            TextView cardData = (TextView) mView.findViewById(R.id.tvData_CardAg);
            cardData.setText(agenda_data);

        }

        public void setAgenda_hora(String agenda_hora){

            TextView cardHora = (TextView) mView.findViewById(R.id.tvHora_CardAg);
            cardHora.setText(agenda_hora);

        }

        public void setStatus_situacao(String status_situacao){

            TextView cardStatus = (TextView) mView.findViewById(R.id.tvStatus_CardAg);
            cardStatus.setText(status_situacao);

        }

        public void setTimestamp_criacaoDt(String timestamp_criacao){

            TextView cardTimeStamp = (TextView) mView.findViewById(R.id.tvTimeStamp);
            cardTimeStamp.setText(timestamp_criacao);

        }

    }
}
TiagoIB
  • 439
  • 7
  • 25

1 Answers1

1

This should work!

Add the getItem(int position) method to your FirebaseRecyclerAdapter as:

@Override
public CardPedidos_row getItem(int position) {
    return super.getItem(getCount() - position - 1);
}

This will return a list in reverse order.

FirebaseRecyclerAdapter<CardPedidos_row, CardPedidosViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<CardPedidos_row,CardPedidosViewHolder>(
CardPedidos_row.class,
R.layout.card_agendamentos_row,
CardPedidosViewHolder.class,   
mDatabaseAgendamentos.child(mCurrentUser.getUid()).orderByChild("status_timeStamp").startAt("Aberto").endAt("Aberto\uf8ff")
) {

@Override
public CardPedidos_row getItem(int position) {
   return super.getItem(getCount() - position - 1);
}

@Override
protected void populateViewHolder(final CardPedidosViewHolder viewHolder, final CardPedidos_row model, int position) { ....
Suhayl SH
  • 1,213
  • 1
  • 11
  • 16