I'm trying to get the list of friends to appear, but only the first one appears. I'm following a tutorial and my code is identical to what 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();
}
}
By debugging the code, both of them appear in Array
:
I'm using Firebase Database
. What can I be doing wrong? I already checked in another tutorial and apparently it looks similar to the code.