Create the database in firebase and I was able to do a single insertion, but when I did it I got the following firebase exception
Can't convert object of type java.lang.String to type com.fundamentos.abisu.chatfirebase.Mensaje
My mistake is supposed to be in this line
Message message = dataSnapshot.getValue (Message.class);
of the main activity
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity {
private CircleImageView fotoPerfil;
private TextView nombre;
private RecyclerView rvMensajes;
private EditText txtMensajes;
private ImageButton btnMensajes;
private MensajeAdapter mensajeAdapter;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
@Nullable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fotoPerfil = (CircleImageView) findViewById(R.id.fotoPerfil);
nombre = (TextView) findViewById(R.id.nombre);
rvMensajes = (RecyclerView) findViewById(R.id.rvMensajes);
txtMensajes = (EditText) findViewById(R.id.txtMensajes);
btnMensajes = (ImageButton) findViewById(R.id.btnMensajes);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("chat");//Sala de chat (nombre)
mensajeAdapter = new MensajeAdapter(this);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
rvMensajes.setLayoutManager(layoutManager);
rvMensajes.setAdapter(mensajeAdapter);
btnMensajes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseReference.push().setValue(new Mensaje(txtMensajes.getText().toString(),nombre.getText().toString(),"","1","00:00"));
}
});
mensajeAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
setScrollbar();
}
});
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
**Mensaje mensaje = dataSnapshot.getValue(Mensaje.class);**
mensajeAdapter.addMensaje(mensaje);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void setScrollbar() {
rvMensajes.scrollToPosition(mensajeAdapter.getItemCount()-1);
}
}
But I do not know what the problem really is, these are my MessageAdpater classes
package com.fundamentos.abisu.chatfirebase;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class MensajeAdapter extends RecyclerView.Adapter<HolderMensaje> {
private List<Mensaje> listMensaje = new ArrayList<>();
private Context context;
public MensajeAdapter(Context context) {
this.context = context;
}
public void addMensaje(Mensaje mensaje){
listMensaje.add(mensaje);
notifyItemInserted(listMensaje.size());
}
@NonNull
@Override
public HolderMensaje onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(context).inflate(R.layout.card_view_mensajes,viewGroup,false);
return new HolderMensaje(v);
}
@Override
public void onBindViewHolder(HolderMensaje holderMensaje, int i) {
holderMensaje.getNombre().setText(listMensaje.get(i).getNombre());
holderMensaje.getMensaje().setText(listMensaje.get(i).getMensaje());
holderMensaje.getHora().setText(listMensaje.get(i).getHora());
}
@Override
public int getItemCount() {
return listMensaje.size();
}
}
my Message class
package com.fundamentos.abisu.chatfirebase;
public class Mensaje {
private String mensaje;
private String nombre;
private String fotoPerfil;
private String tipoMensaje;
private String hora;
public Mensaje() {
}
public Mensaje(String mensaje, String nombre, String fotoPerfil, String tipoMensaje, String hora) {
this.mensaje = mensaje;
this.nombre = nombre;
this.fotoPerfil = fotoPerfil;
this.tipoMensaje = tipoMensaje;
this.hora = hora;
}
public String getMensaje() {
return mensaje;
}
public void setMensaje(String mensaje) {
this.mensaje = mensaje;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getFotoPerfil() {
return fotoPerfil;
}
public void setFotoPerfil(String fotoPerfil) {
this.fotoPerfil = fotoPerfil;
}
public String getTipoMensaje() {
return tipoMensaje;
}
public void setTipoMensaje(String tipoMensaje) {
this.tipoMensaje = tipoMensaje;
}
public String getHora() {
return hora;
}
public void setHora(String hora) {
this.hora = hora;
}
}
my HolderMessage class
package com.fundamentos.abisu.chatfirebase;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import de.hdodenhof.circleimageview.CircleImageView;
public class HolderMensaje extends RecyclerView.ViewHolder {
private TextView nombre, mensaje, hora;
private CircleImageView fotoMensaje;
public HolderMensaje(View itemView) {
super(itemView);
nombre = (TextView) itemView.findViewById(R.id.msgNombre);
mensaje = (TextView) itemView.findViewById(R.id.msgMensaje);
hora = (TextView) itemView.findViewById(R.id.msgHora);
fotoMensaje = (CircleImageView) itemView.findViewById(R.id.msgFotoPerfil);
}
public TextView getNombre() {
return nombre;
}
public void setNombre(TextView nombre) {
this.nombre = nombre;
}
public TextView getMensaje() {
return mensaje;
}
public void setMensaje(TextView mensaje) {
this.mensaje = mensaje;
}
public TextView getHora() {
return hora;
}
public void setHora(TextView hora) {
this.hora = hora;
}
public CircleImageView getFotoMensaje() {
return fotoMensaje;
}
public void setFotoMensaje(CircleImageView fotoMensaje) {
this.fotoMensaje = fotoMensaje;
}
}
This is the capture of my firebase database with the only record I could do
These are the rules of the database