Change item color - Recyclerview and Firebase

Asked

Viewed 374 times

0

I need to change the color of a Linearlayout, as a query to Firebase.

Bank structure:

USUARIOS
- ID_USUARIO
---AGENDAMENTOS
-----nome: João
-----lastview_usuario: "1506816888384"
-----timestamp_update: "1506816951405"

I am recovering the data and displaying in a Recyclerview/Cardview. It is working properly.

Problem:

I have a Linearlayou on top of Cardview. inserir a descrição da imagem aqui

I would like to compare the fields lastview_usuario and timestamp_update. If different, set a color for the background. In this case, black.

I created the method setNotificationCard() to make the background change.

Something is wrong. According to my code, it updates only the first data in the list.

For example: This colorful item, has its timestamp_update and lastview_user, identical. Lot should not colour.

Code:

public class PedidosTab1 extends Fragment {

    private RecyclerView mCardPedidos;
    private DatabaseReference mDatabaseUsuarios;
    private DatabaseReference mDatabaseAgendamentos;

    private FirebaseAuth mAuth;
    private FirebaseUser mCurrentUser;

    private TextView mTextPadrao;



    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();

        mDatabaseUsuarios = FirebaseDatabase.getInstance().getReference().child("Usuarios").child(mCurrentUser.getUid());
        mDatabaseAgendamentos = mDatabaseUsuarios.child("Agendamentos");

        /*Offline*/
        //mDatabaseAgendamentos.keepSynced(true);

        /*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));


        /*Validação pedidos existentes*/
        mDatabaseUsuarios.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot.child("Agendamentos").exists()){

                    carregarDadosRecycler();

                } else {

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

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });



        return view;
    }


    private void carregarDadosRecycler() {

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

                //mDatabaseAgendamentos = mDatabaseUsuarios.child("Agendamentos");

                VendasEmpresas.class,
                R.layout.card_agendamentos_row,
                CardPedidosViewHolder.class,
                mDatabaseAgendamentos.orderByChild("status_timestamp").startAt("Aberto").endAt("Aberto\uf8ff")


        ) {

            /*Ajuda para ordenar dados em forma decrescente*/
            @Override
            public VendasEmpresas getItem(int position) {
                return super.getItem(getItemCount() - position - 1);
            }

            @Override
            protected void populateViewHolder(final CardPedidosViewHolder viewHolder, final VendasEmpresas 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.getSituacao();
                final String timeStamp = model.getTimestamp_updateDt();



                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.setSituacao(model.getSituacao());
                viewHolder.setTimestamp_updateDt(model.getTimestamp_updateDt());

                viewHolder.setNotificationCard(pedido_key);

                /*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);




                    }
                });



            }
        };

        mCardPedidos.setAdapter(firebaseRecyclerAdapter);



    }

    public static class CardPedidosViewHolder extends RecyclerView.ViewHolder{

        View mView;

        LinearLayout mLinearLayoutNotif;

        DatabaseReference mDatabaseNotficacao;

        FirebaseAuth mAuth;
        FirebaseUser mCurrentUser;

        public CardPedidosViewHolder ( View itemView ){

            super(itemView);

            mView = itemView;

            mLinearLayoutNotif = (LinearLayout) mView.findViewById(R.id.cardAg_linearLayout1);

            mDatabaseNotficacao = FirebaseDatabase.getInstance().getReference().child("Usuarios");

            mAuth = FirebaseAuth.getInstance();

        }


        public void setNotificationCard( final String pedido_key ){

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

            mDatabaseNotficacao.child(mCurrentUser.getUid()).child("Agendamentos").child(pedido_key).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    String lastView_usuario = dataSnapshot.child("lastview_usuario").getValue(String.class);
                    String timestamp_update = dataSnapshot.child("timestamp_update").getValue(String.class);

                    Long s1 = new Long(lastView_usuario);
                    Long s2 = new Long(timestamp_update);

                    if ( !s1.equals(s2) ){

                        mLinearLayoutNotif.setBackgroundColor(Color.BLACK);

                    } else {



                    }


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });


        }

        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 setSituacao(String situacao){

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

        }


        public void setTimestamp_updateDt(String timestamp_updateDt){

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

    }

}
  • in Else a set color white, see if you get the expected result.

  • It’s still random. @Edsonreis

No answers

Browser other questions tagged

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