Recover Data from Firebase in Recyclerview

Asked

Viewed 1,389 times

0

I need to recover from Firebase in Recyclerview user order requests in a way that organizes these requests in a list without overwriting the other one. For now my list is static.

Each user has an id in the BD and each request 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;
}

}

1 answer

1

Try to use Firebaseui: https://github.com/firebase/FirebaseUI-Android

It’s much simpler than creating your own Recyclerview.

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

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

Then 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, on your Activity, just call the recyclerView from Firebaseui with your model. 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 the syntax for Java or let Android Studio convert.

  • But how will this information transition? Type the user App data into the company App?

  • What is the purpose of your code? I understood that you were having problems with your Recyclerview. Your question is a little poorly worded.

  • I made a mistake with another question I had asked in the forum. This already manage to solve, obg by help.

  • The other question is: https://answall.com/questions/294960/troca-de-informa%C3%A7%C3%B5es-entre-recycleviews. Vlw

Browser other questions tagged

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