Display User Information (FireBase, Android)

0

Good, I have a BBDD in FireBase with its Users, and some Notices, each section has its information etc.

I have managed to show the Notices (about the User that is logged in the APP), now I need to show the Information about the User that is logged in, but I can not make it work. Since after entering User, look for the Uid, and I go back to find another Uid which says it is Last Name and it is when the error jumps, since there is no other Uid, which would have to change to get it to work? / p>

Thank you very much

Code Show Users

public class VerUsuario extends Registro
{

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    FirebaseUser user = firebaseAuth.getInstance().getCurrentUser();

    List<Usuario> usuarios;
    RecyclerView recycler;
    RecyclerView.Adapter adapter;

//    Button atras;
//    Button editar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.verusuario);

//        atras = (Button) findViewById(R.id.atras);
//        editar = (Button) findViewById(R.id.editar);

        usuarios = new ArrayList<>();

        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        adapter = new UserAdapter(usuarios);
        recycler.setAdapter(adapter);

        database.getReference("Usuario").child(user.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                usuarios.clear();
                for(DataSnapshot snapshot :
                    dataSnapshot.getChildren())
                {
                    Log.e("Hola, error!,",snapshot.toString());
                    Usuario usuario2 = snapshot.getValue(Usuario.class);
                    usuarios.add(usuario2);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

** Adapter Code **

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder>
{
    private List<Usuario> usuarios;

    public UserAdapter(List<Usuario> usuarios)
    {
        this.usuarios = usuarios;
    }

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

    @Override
    public void onBindViewHolder(UserViewHolder holder, int position) {
        Usuario users = usuarios.get(position);
        holder.nombre.setText(users.getNombre());
        holder.apellidos.setText(users.getApellidos());
        holder.dni.setText(users.getDni());
        holder.email.setText(users.getDni());
    }


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

    public class UserViewHolder extends RecyclerView.ViewHolder
    {
        public TextView nombre;
        public TextView apellidos;
        public TextView dni;
        public TextView email;

        public UserViewHolder(View v) {
            super(v);
            nombre = (TextView) v.findViewById(R.id.nombre);
            apellidos = (TextView) v.findViewById(R.id.descripcion);
            dni = (TextView) v.findViewById(R.id.ubicacion);
            email = (TextView) v.findViewById(R.id.email);
        }
    }
}
    
asked by Cristian Prieto Beltran 02.05.2017 в 10:06
source

1 answer

1

You are cycling your nodo of the id of your user . You must eliminate the cycle because you already have the reference of your user . That is, database.getReference("Usuario").child(user.getUid())

Try this:

database.getReference("Usuario").child(user.getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            usuarios.clear();

             Usuario usuario2 = dataSnapshot.getValue(Usuario.class);
             usuarios.add(usuario2);

            adapter.notifyDataSetChanged();
        }

If you want to get information from each user then do this:

database.getReference("Usuario").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                usuarios.clear();
                for(DataSnapshot snapshot :
                    dataSnapshot.getChildren())
                {
                    Usuario usuario2 = snapshot.getValue(Usuario.class);
                    usuarios.add(usuario2);
                }
                adapter.notifyDataSetChanged();
            }
    
answered by 02.05.2017 / 10:34
source