Recyclerview Error: No layout manager Attached

Asked

Viewed 1,280 times

3

I couldn’t find the mistake.

my Activity:

private AdapterPacientes adapterPacientes;
private static MVP.Presenter presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_pacientes);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    if (presenter == null)
        presenter = new Presenter();
    presenter.setActivity(this);
    presenter.retrivePacientes( savedInstanceState );
}

@Override
protected void onStart() {
    super.onStart();

    RecyclerView lista = (RecyclerView) findViewById(R.id.list);
    lista.setHasFixedSize(true);

    adapterPacientes = new AdapterPacientes(presenter.getPacientes());
    lista.setAdapter(adapterPacientes);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putParcelableArrayList(KEY_PACIENTE, presenter.getPacientes());
    super.onSaveInstanceState(outState);
}

@Override
public void refreshAdapter() {
    adapterPacientes.notifyDataSetChanged();
}

// ...

error that appears in the log

erro do log

I spent more than two hours researching but I couldn’t find why... The code structure is in the MVP standard.

The data is being brought in and the arraylist being properly populated.

thank you in advance.

2 answers

5


The error is illuminating: No associated Layoutmanager.

One of differences between Recyclerview and Listview is that Recyclerview is agnostic regarding how the items are visually arranged.
This responsibility is carried out by the associated Layoutmanager.

So what’s left is to associate one with Recyclerview. Android’s API provides several Layoutmanager and you can create your own.

@Override
protected void onStart() {
    super.onStart();

    RecyclerView lista = (RecyclerView) findViewById(R.id.list);
    lista.setHasFixedSize(true);

    //Atribuir LayoutManager
    lista.setLayoutManager(new LinearLayoutManager(this));

    adapterPacientes = new AdapterPacientes(presenter.getPacientes());
    lista.setAdapter(adapterPacientes);
}
  • 1

    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

0

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;
}

}

Browser other questions tagged

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