Returning data from Firebase to a Listview

Asked

Viewed 127 times

0

I’m making an app for a subject in college that consists of creating events, but I’m having trouble returning these events in a Listview. I saw some similar questions, copied many results but nothing that worked so far. If anyone can give a help

Foto do banco de dados

Event Class:

public Evento(){

}

public String getTitulo() {
    return titulo;
}

public void setTitulo(String titulo) {
    this.titulo = titulo;
}

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

public String getHorario() {
    return horario;
}

public void setHorario(String horario) {
    this.horario = horario;
}

public String getEndereco() {
    return endereco;
}

public void setEndereco(String endereco) {
    this.endereco = endereco;
}

public String getNumEnd() {
    return numEnd;
}

public void setNumEnd(String numEnd) {
    this.numEnd = numEnd;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

Eventoadapter class:

public class EventoAdapter extends ArrayAdapter<Evento> {
private ArrayList<Evento> arrayList;
private Context context;

public EventoAdapter(@NonNull Context c, @NonNull ArrayList<Evento> objects){
    super(c,0,objects);
    this.arrayList = objects;
    this.context = c;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
    View view = null;

    if (arrayList != null){
        LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.item,parent,false);

        TextView titulo = view.findViewById(R.id.txtTitulo);
        TextView data = view.findViewById(R.id.txtData);
        TextView horario = view.findViewById(R.id.txtHorario);
        TextView endereco = view.findViewById(R.id.txtEndereco);
        TextView numEnd = view.findViewById(R.id.txtNumEnd);
        TextView descricao = view.findViewById(R.id.txtDescricao);

        Evento evento = arrayList.get(position);
        titulo.setText(evento.getTitulo());
        data.setText(evento.getData());
        horario.setText(evento.getHorario());
        endereco.setText(evento.getEndereco());
        numEnd.setText(evento.getNumEnd());
        descricao.setText(evento.getDesc());
    }
    return view;

}

Listings

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listar_eventos);

    arrayList = new ArrayList<>();
    listView = findViewById(R.id.lista_eventos);
    adapter = new EventoAdapter(ListarEventos.this, arrayList);
    listView.setAdapter(adapter);

    eventoReferencia.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            arrayList.clear();

            for(DataSnapshot s: dataSnapshot.getChildren()){
                Evento evento = s.child("titulo").getValue(Evento.class);
                arrayList.add(evento);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}
  • 1

    I suggest using a Recyclerview https://developer.android.com/guide/topics/ui/layout/recyclerview

  • Event = s.Child("title"). getValue(Event.class); why you are using s.Child("title") and this firebase field is a string by its example, I believe you should use s.getValue(Event.class); to find out which knot you’re using the Istener

No answers

Browser other questions tagged

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