Listview displays only 1 item

Asked

Viewed 275 times

0

I’m trying to bring the list of friends to be displayed, but only the first registered appears. I’m following a tutorial and my code is identical to the one I’m following.

public class ListaAmigosActivity extends AppCompatActivity {
ListView lsvAmigos;

FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;

private List<Amigos> listAmigos = new ArrayList<Amigos>();
private ArrayAdapter<Amigos> arrayAdapterAmigos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_amigos);
    lsvAmigos = (ListView)findViewById(R.id.lsvAmigos);
    inicializaFirebase();
    mostraAmigos();
}

private void mostraAmigos() {
    databaseReference.child("Amigos").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //listAmigos.clear();
            for(DataSnapshot objSnapshot:dataSnapshot.getChildren()){
                Amigos amigos = objSnapshot.getValue(Amigos.class);
                listAmigos.add(amigos);
            }
            arrayAdapterAmigos = new ArrayAdapter<Amigos>(ListaAmigosActivity.this,
                    android.R.layout.simple_list_item_1,listAmigos);
            lsvAmigos.setAdapter(arrayAdapterAmigos);
            alert("Mensagem exibida");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

private void alert(String s) {
    Toast.makeText(ListaAmigosActivity.this, s, Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_voltar,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();//pega id que foi clicado (botão salvar)

    //verifica se foi clicado no botão salvar
    if(id== R.id.menuVoltar){
        //Instancia a activity para ir à lista de amigos
        Intent i = new Intent(ListaAmigosActivity.this, PerfilActivity.class);
        startActivity(i);//chama a lista de amigos
    }
    return true;
}
private void inicializaFirebase() {
    FirebaseApp.initializeApp(ListaAmigosActivity.this);
    firebaseDatabase = FirebaseDatabase.getInstance();
//    firebaseDatabase.setPersistenceEnabled(true);
    databaseReference = firebaseDatabase.getReference();
}
}

Debugging the code appears both in the Array: inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

I’m using the Firebase Database. What could I be doing wrong? I’ve checked another tutorial and it looks similar to the code.

  • I was looking at some old codes of mine, and apparently it’s the same, I just use Recyclerview instead, and I already add an Adapter with an empty list at the beginning of everything. When loading the data, I update that Adapter and use Notifydatasetchanged() to notify Recycler to update.

  • I ended up discovering the mistake, but I forgot to finish it.

1 answer

0


I found the solution to my problem, which was actually in the Layout, because he was using the ListView within a ScrollView, and for some reason that I don’t know for sure doesn’t work, but I realized that the ListView has a Scroll.

Browser other questions tagged

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