Populate Arraylist dynamically ANDROID STUDIO

Asked

Viewed 499 times

0

The main screen of my app consists of a few sessions using Recyclerview. For my recyclerview Adapter I pass an arraylist of type Sessoes for the screen to look the way I want. But this arraylist I can only fill statically. I’d like to fill it out dynamically just by adding a field in firebase, so I don’t have to fiddle with the code every time I want to add or delete a session. Below follow the screen and the codes. I use firebase as backend.

Class Sessions:

public class Sessoes {
private String nomeSessao;
private String descricaoSessao;
private String nomeSessaoBD;
private String img;

public Sessoes (){

}

public Sessoes(String nome, String desc, String nomeBD){
    nomeSessao = nome;
    descricaoSessao = desc;
    nomeSessaoBD = nomeBD;
}

public String getNomeSessao() {
    return nomeSessao;
}

public void setNomeSessao(String nomeSessao) {
    this.nomeSessao = nomeSessao;
}

public String getDescricaoSessao() {
    return descricaoSessao;
}

public void setDescricaoSessao(String descricaoSessao) {
    this.descricaoSessao = descricaoSessao;
}

public String getNomeSessaoBD() {
    return nomeSessaoBD;
}

public void setNomeSessaoBD(String nomeSessaoBD) {
    this.nomeSessaoBD = nomeSessaoBD;
}

public String getImg() {
    return img;
}

public void setImg(String img) {
    this.img = img;
    }
}

Code for filling the arraylist:

public List<Sessoes> todasAsSessoes() {

    List<Sessoes> sessoes = new ArrayList<>();

    Sessoes sessoes_1 = new Sessoes("Próxima Ação","Informações sobre a próxima ação a ser realizada.", "proxima_acao" );
    Sessoes sessoes_2 = new Sessoes("Galeria", "Fotos das ações já realizadas.", "galeria");
    Sessoes sessoes_3 = new Sessoes("Quero Participar", "Quer ser voluntário em alguma ação?", "quero_participar");
    Sessoes sessoes_4 = new Sessoes("Quero Doar", "O que você quer doar?", "quero_doar");
    Sessoes sessoes_5 = new Sessoes("Calendário", "Confira o calendário das nossas ações.", "calendario");
    Sessoes sessoes_6 = new Sessoes("Locais atendidos", "Confira os locais já visitados pelo Doe Amor", "locais_atendidos");
    Sessoes sessoes_7 = new Sessoes("Contatos","Fale conosco!", "contatos");
    Sessoes sessoes_8 = new Sessoes("Redes Sociais", "Junte-se a nós e espalhe o amor pelas suas redes", "redes_sociais");

    Sessoes[] sessoesAdd = {sessoes_1, sessoes_2, sessoes_3, sessoes_4, sessoes_5, sessoes_6, sessoes_7, sessoes_8};

    for (int i=0 ; i <sessoesAdd.length ; i++){
        sessoes.add(sessoesAdd[i]);        }

    return sessoes;
}

It is precisely this code above that I need to change to be filled dynamically.

Arraylist Pass Code for Recyclerview Adapter:

 sessoes = todasAsSessoes();

    recyclerView = (RecyclerView) findViewById(R.id.recycler);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

    recyclerView.setLayoutManager(layoutManager);

    AdapterSessoes adapter = new AdapterSessoes(sessoes, this);

    recyclerView.setAdapter(adapter);

    recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

Screen of how the sessions are arranged:

Tela das sessões

1 answer

0


Come on...

The first thing in mind is to have to upload the Firebase data.

Remembering that this is not the best way to access your database. It’s just the most rapid from the point of view of implementation.

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            ...
           ValueEventListener listener = new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                   for(DataSnapshot snapshot : dataSnapshot) {
                       mListaSessoes.add(snapshot.getValue(Sessao.class));
                   }
                   mSessaoAdapter.setListaSessoes(mListaSessoes);           
              }
              @Override
              public void onCancelled(DatabaseError databaseError) {
                  // Aconteceu algo de errado
              }
           };
           mFirebaseReference.child("sessoes")
                 .addValueEventListener(listener);
        }
    }

Then make your Adapter initialize the session list for one setter

public void setListaSessoes(@NonNull ArrayList<Sessao> listaSessoes) {
       this.sessoes = listaSessoes;
}
  • Man, sorry I’m just a beginner. I’ve already read the firebase documentation and run some tests. But here I could not identify how I will fill the data in the object. I have 4 attributes in it that are. As you put it, if I understand correctly, it will already load the 4 firebase values on the object, but how will it know which attribute to put?

  • Sorry for the delay in answering. Anyway, I don’t know if you’ve solved your problem, but let’s answer your question: when you call getValue(Sessao.class), the method already maps the properties coming from the database based on the names of the getters and setters of your model (Sessao.class). For example: if I have one getName(), getNumber() and getAddress() it already maps properties in the database as name, number, address; and when you recover the data by passing your model, it already knows where to set the values: setName(), setNumber() and setAddress()

  • So the names in the database have to be the same as the ones in my class?

  • That’s right @Marceloportela. Still doubt?

  • Manoooo WAS WORTH TOO MUCH, I managed to solve here.... Thanks Ivan Silva

  • I’m happy to help @Marceloportela! If your problem has been solved thanks to my answer, please accept it as correct.

Show 1 more comment

Browser other questions tagged

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