Recover Firebase Data in RecyclerView

0

I need to retrieve user request requests from the Firebase in the RecyclerView so that they organize those requests in the list without overwriting the other. For now my list is static.

Each user has an id in the DB and each request has tbm.

Adapter

     public class AdapterSolicitacoes extends RecyclerView.Adapter <AdapterSolicitacoes.MyViewHolder> {

    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView bairro;
        TextView endereco;
        TextView data;

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

            bairro = itemView.findViewById(R.id.tv_bairro);
            endereco = itemView.findViewById(R.id.tv_endereco);
            data = itemView.findViewById(R.id.tv_data);
        }
    }

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemLista = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_lista_solicitacao, parent, false);

    return new MyViewHolder(itemLista);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.bairro.setText("ITAJAI");
        holder.endereco.setText("Rua JOAO DE BARRO, 222.");
        holder.data.setText("27 Abr 18");

}

@Override
public int getItemCount() {
    return 10;
}

}

Fragment

     public class SolicRecebidaFragment extends Fragment {

private RecyclerView recyclerViewListaSolicitacao;
private DatabaseReference referencia ;


public SolicRecebidaFragment() {
    // 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_solicitacao_recebida, container, false);

    //Config.Inciais
    recyclerViewListaSolicitacao = view.findViewById(R.id.recyclerView_ListaSolicitacao);

    //Config.Adapter
    AdapterSolicitacoes adapter = new AdapterSolicitacoes();

    //Config.RecyclerView
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerViewListaSolicitacao.setLayoutManager( layoutManager);
    recyclerViewListaSolicitacao.setHasFixedSize(true);
    recyclerViewListaSolicitacao.setAdapter( adapter );

    //Recupera endereço do nó usuarios
    referencia = ConfiguracaoFirebase.getFirebaseDataBase().child("usuarios");
    referencia.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.i("DADOS FIREBASE: ", dataSnapshot.getValue().toString());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


return view;
}

}

    
asked by anonymous 27.04.2018 / 09:32

1 answer

1

Try using FirebaseUI: link

It's much simpler than just creating your own RecyclerView.

First, you add the dependency in your build.gradle. As follows:

// FirebaseUI for Firebase Realtime Database
implementation 'com.firebaseui:firebase-ui-database:3.3.1'

Next, you create a template for the data you want to use.

For example, the teacher model.

internal class Professor {

var nome: String? = null
var email: String? = null
var picture: String? = null
var description: String? = null
var lattes: String? = null
var areas: String? = null

fun Professor(nome: String, email: String) {
    this.nome = nome
    this.email = email
}

fun Professor() {} }

Then in your activity, just call the recyclerView of FirebaseUI with your template. As follows:

        val adapter = object : FirebaseRecyclerAdapter<Professor, MessageViewHolder>(
            Professor::class.java,
            R.layout.item_professores_unit,
            MessageViewHolder::class.java,
            myRef
    ) {
        override fun populateViewHolder(viewHolder: MessageViewHolder, model: Professor, position: Int) {//Fazer o que quiser com os dados recebidos. }

Note that the codes are in Kotlin, but the idea is very similar to Java. Just use syntax for Java or let Android Studio convert.

    
29.04.2018 / 21:35