Hello, in my case I made the association of Layoutmanager, but keeps giving the error!!
Method to list firebase data
public void listarContatos(){
//Listagem de Contatos
contatosRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Contato contatos = dataSnapshot.getValue(Contato.class);
listaContatos.add(contatos);
Adapter adaptador = new Adapter(listaContatos);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
listViewContatos.setLayoutManager(layoutManager);
listViewContatos.setHasFixedSize(true);
listViewContatos.setAdapter(adaptador);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}insira o código aqui
Adapter class
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>{
private List <Contato> listaContatos;
public Adapter(List<Contato> lista) {
this.listaContatos = lista;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemLista = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contatos, parent, false);
return new MyViewHolder(itemLista);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Contato contatos = listaContatos.get(position);
holder.nome.setText(contatos.getNome());
holder.telefone.setText(contatos.getTelefone());
holder.celular.setText(contatos.getCelular());
holder.email.setText(contatos.getEmail());
holder.status.setImageResource(contatos.getImageStatus());
}
@Override
public int getItemCount() {
return this.listaContatos.size();
}
//MINHA VIEW HOLDER ****************************************************************************
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView nome;
TextView telefone;
TextView celular;
TextView email;
ImageView status;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
nome = itemView.findViewById(R.id.textNome);
telefone = itemView.findViewById(R.id.textTelefone);
celular = itemView.findViewById(R.id.textTelefone);
email = itemView.findViewById(R.id.textEmail);
status = itemView.findViewById(R.id.imageStatus);
}
}
}
Model Class
public class Contact {
private String nome, celular, telefone, email;
private int imageStatus;
private Contato contato;
public Contato() {
}
public Contato(String nome, String celular, String telefone, String email) {
this.nome = nome;
this.celular = celular;
this.telefone = telefone;
this.email = email;
}
public Contato(Contato contato) {
this.contato = contato;
}
public void salvar(){
FirebaseAuth autenticacao = ConfigFirebase.getFirebaseAutenticacao();
String idUsuario = autenticacao.getCurrentUser().getUid().toString();
DatabaseReference firebase = ConfigFirebase.getFirebase();
firebase.child("contatos")
.child(idUsuario)
.push()
.setValue(this);
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCelular() {
return celular;
}
public void setCelular(String celular) {
this.celular = celular;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Exclude
public int getImageStatus() {
return imageStatus;
}
public void setImageStatus(int imageStatus) {
this.imageStatus = imageStatus;
}
}
Wow... I’m ashamed I didn’t see that I didn’t declare Layoutmanager... That’s why when the mind starts to shuffle it’s best to take a break, get some sleep or play a game... Then it gives a reset in the mind and we think better! Rsrs
– Rafael Silva