Change color item - RecyclerView and Firebase

0

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

Bank Structure:

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

I am retrieving the data and displaying it in a RecyclerView / Cardview. It is working properly.

Problem:

I have a LinearLayou at the top of CardView.

Iwouldliketocomparethelastview_userandtimestamp_updatefields.Ifitisdifferent,setacolorforthebackground.Inthatcase,black.

IcreatedthesetNotificationCard()methodtomakethebackgroundchange.

Somethingiswrong.Accordingtomycode,itonlyupdatesthefirstdatainthelist.

Forexample:Thiscolorfulitemhasitstimestamp_updateandlastview_user,identical.Itwasnotsupposedtocolor.

Code:

publicclassPedidosTab1extendsFragment{privateRecyclerViewmCardPedidos;privateDatabaseReferencemDatabaseUsuarios;privateDatabaseReferencemDatabaseAgendamentos;privateFirebaseAuthmAuth;privateFirebaseUsermCurrentUser;privateTextViewmTextPadrao;publicPedidosTab1(){//Requiredemptypublicconstructor}@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){//InflatethelayoutforthisfragmentViewview=inflater.inflate(R.layout.fragment_pedidos_tab1,container,false);/*RecuperandoinstanciadoFirebase*/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);
        }

    }

}
    
asked by anonymous 01.10.2017 / 21:19

0 answers