Problem loading a RecyclerView with Firebase data (No setter / field)

0

I need to fill a recyclerview with the data I have stored in FireBase but I can not find it and I am not able to find the error.

The firebase json

"aviso" : {
"-LT9trd7ej8aLhPlDVsM" : {
  "latitud" : 37.7,
  "longitud" : -122.0999983,
  "usuario" : "login2"
},
"-LT9ty7hc9si6x2IempD" : {
  "latitud" : 37.7,
  "longitud" : -122.0999983,
  "usuario" : "login2"
},
"correoprueba" : {
  "latitud" : 0,
  "longitud" : 0,
  "usuario" : "correoprueba"
},
"login2" : {
  "latitud" : 32,
  "longitud" : -122.0999983,
  "usuario" : "login2"
}
},
"chat" : {
"-LT9WsU8whS5ZPONAosl" : {
  "fotoPerfil" : "",
  "hora" : 1544217397371,
  "mensaje" : "hola que tal",
  "nombre" : "Hola ",
  "typemensaje" : "1"

} }

This is my adapter

public class AdapterUsuario extends RecyclerView.Adapter<AdapterUsuario.UsuarioHolder>{

List<Aviso> avisos;
Context context;

public AdapterUsuario(List<Aviso> avisos) {
    this.avisos = avisos;
}

@Override
public UsuarioHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_aviso,parent,false);
    UsuarioHolder holder=new UsuarioHolder(v);
    return holder;
}

@Override
public void onBindViewHolder(UsuarioHolder holder, int position) {
    Aviso user = avisos.get(position);
    holder.latitud.setText(String.valueOf(user.getLatitud()));
    holder.longitud.setText(String.valueOf(user.getLongitud()));
    //holder.getNombre().setText(listMensaje.get(position).getNombre());
    holder.usuario.setText(user.getUsuario());
}

@Override
public int getItemCount() {
    return avisos.size();
}

public static class UsuarioHolder extends RecyclerView.ViewHolder{
    TextView usuario,longitud,latitud;
    public UsuarioHolder(View itemView) {
        super(itemView);
        usuario=itemView.findViewById(R.id.tvUsuario);
        longitud=itemView.findViewById(R.id.tvLongitud);
        latitud=itemView.findViewById(R.id.tvLatitud);
    }
}
}

Object

public class Aviso {
private Double latitud;
private Double longitud;
private String usuario;

public Aviso() {
}

public Aviso(Double latitud, Double longitud, String usuario) {
    this.latitud = latitud;
    this.longitud = longitud;
    this.usuario = usuario;
}

public Double getLatitud() {
    return latitud;
}

public void setLatitud(Double latitud) {
    this.latitud = latitud;
}

public Double getLongitud() {
    return longitud;
}

public void setLongitud(Double longitud) {
    this.longitud = longitud;
}

public String getUsuario() {
    return usuario;
}

public void setUsuario(String usuario) {
    this.usuario = usuario;
}

}

This is the activity

public class AvisosActivity extends AppCompatActivity {

RecyclerView rv;
List<Aviso> aviso;
AdapterUsuario adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_avisos);

    rv= (RecyclerView) findViewById(R.id.rvAvisos);

    rv.setLayoutManager(new LinearLayoutManager(this));

    aviso=new ArrayList<>();

    FirebaseDatabase database = FirebaseDatabase.getInstance();

    adapter = new AdapterUsuario(aviso);

    rv.setAdapter(adapter);

    database.getReference().getRoot().addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            aviso.removeAll(aviso);
            for (DataSnapshot snapshot:
                    dataSnapshot.getChildren()){
                Aviso user = snapshot.getValue(Aviso.class);
                aviso.add(user);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

}

Thank you very much, I'm blocked with this topic

    
asked by Carlos 09.12.2018 в 19:09
source

1 answer

0

You still have to ask for the data in the for because

aviso.add(user); does not set the values to the model you need

for that he does the following

  for (DataSnapshot snapshot:
                    dataSnapshot.getChildren()){
                Aviso user = snapshot.getValue(Aviso.class);
                user.setLatitud(user.getLatitud());
                user.setLongitud(user.getLongitud());
                user.setUsuario(user.getUsuario());
                aviso.add(user);
            }

Also the reference you take is wrong because database.getReference().getRoot(). is going to return the root node and not the node you are looking for the data, to find the data you should go to where are those data that you set in the model

database.getReference().child("aviso").child(ID).addValueEventListener...

where. child(ID) is the node that you want to search for that data within the warning

    
answered by 10.12.2018 / 18:28
source