I'm doing a chat, where I charge it through a RecyclerView. When I enter the chat, I should load the messages, but it does not, and I try to scroll in that recyclerView and I get the following message every time I scroll:
RecyclerView: No adapter attached; skipping layout
I do not know what could be causing this error, look in different sides, but each person has a different solution.
This is my Activity where the chat is
public class ChatActivity extends AppCompatActivity {
private TextView nombreperfil;
private ImageView imagenperfil;
private RecyclerView mensajes;
private EditText texto;
private Button enviar;
private ImageButton buscarimg;
private CardView globo;
private mensajeAdaptador adaptador;
ArrayList<ModelChat> mensajesArray = new ArrayList<>();
String ruta = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
nombreperfil = (TextView) findViewById(R.id.nombreperfil);
imagenperfil = (ImageView) findViewById(R.id.imagenperfil);
mensajes = (RecyclerView) findViewById(R.id.mensajes);
texto = (EditText) findViewById(R.id.escribir);
enviar = (Button) findViewById(R.id.enviar);
//globo = (CardView) findViewById(R.id.cardViewMensaje).setForegroundGravity(5);
buscarimg = (ImageButton) findViewById(R.id.imagenbuscar);
LinearLayoutManager l = new LinearLayoutManager(this);
mensajes.setLayoutManager(l);
mensajes.setAdapter(adaptador);
Toolbar toolbar = findViewById(R.id.toolbarchat);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Loperso");
getSupportActionBar().setIcon(getDrawable(R.drawable.heart_full));
//Cargo los mensajes cuando entra al activity
ArrayList<Mensaje> listamensajes = new ArrayList<>();
listamensajes = DAO.cargarMensajes(0);
for (int i = 0; i < listamensajes.size(); i++) {
String mnss = listamensajes.get(i).getContenidotexto();
Date fech = listamensajes.get(i).getFecha();
String idusuario = listamensajes.get(i).getCedulaUsuario();
mensajesArray.add(new ModelChat(ModelChat.TEXTO_TYPE, listamensajes.get(i)));
}
añadirMensaje();
//ultimaPos();
/**adaptador.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
//ultimaPos();
}
});**/
//Cargo los mensajes justo cuando entra al activity
enviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Mensaje msm = new Mensaje(DAO.obtenerIdACrearMensaje(), String.valueOf(texto.getText()), DAO.obtenerHora(), false, "0", Login.usuario.getCedula(), "0", null);
//Mensaje que se envia a la base de datos
DAO.insertarMensaje(msm);
mensajesArray.add(new ModelChat(ModelChat.TEXTO_TYPE, msm));
añadirMensaje();
}
});
}
public void añadirMensaje() {
mensajeAdaptador ms = new mensajeAdaptador(mensajesArray, this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);
mensajes.setLayoutManager(linearLayoutManager);
mensajes.setItemAnimator(new DefaultItemAnimator());
mensajes.setAdapter(adaptador);
}}
This is the Adapter of the messages. I'm using ViewType because I need to load another Layout in that same recyclerView, in addition to the images that have another different structure.
public class mensajeAdaptador extends RecyclerView.Adapter{
private ArrayList<Mensaje> lista = new ArrayList<>();
private ArrayList<ModelChat> mDataset;
private int tipomensaje=0;
private int valorCotizacion;
private Context contexto;
private CardView globo;
public mensajeAdaptador(ArrayList<ModelChat> data, Context context) {
mDataset = new ArrayList<>();
mDataset = data;
this.contexto = context;
}
public static class HolderMensaje extends RecyclerView.ViewHolder{
//Elementos del Mensaje_View
View mView;
TextView mensaje;
TextView hora;
LinearLayout tarjeta;
public HolderMensaje(@NonNull View itemView) {
super(itemView);
mView = itemView;
hora = (TextView) itemView.findViewById(R.id.horamensaje);
mensaje = (TextView) itemView.findViewById(R.id.mensajetexto);
tarjeta = (LinearLayout) itemView.findViewById(R.id.layoutmensaje);
}
public void colocarDerecha(){
hora.setGravity(5);
tarjeta.setGravity(5);
}
public void colocarIzquierda(){
hora.setGravity(3);
tarjeta.setGravity(3);
}
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 static class HolderCotizacion extends RecyclerView.ViewHolder{
TextView valor;
TextView hora;
public HolderCotizacion(View itemView) {
super(itemView);
this.valor = (TextView) itemView.findViewById(R.id.valorcotizacion);
this.hora = (TextView) itemView.findViewById(R.id.horamensajecotizacion);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view;
switch (viewType){
case ModelChat.TEXTO_TYPE:{
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mensaje_view, parent, false);
return new HolderMensaje(view);
}
case ModelChat.COTIZACION_TYPE:{
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cotizacionmensaje_view, parent, false);
return new HolderCotizacion(view);
}
case ModelChat.IMAGEN_TYPE:{
}
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ModelChat object = mDataset.get(position);
if (object != null) {
switch (object.type) {
case ModelChat.TEXTO_TYPE:{
if (mDataset.get(position).mensaje.getCedulaUsuario().equals(Login.usuario.getCedula())){
}else{
}
HolderMensaje hold = (HolderMensaje) holder;
hold.mensaje.setText(mDataset.get(position).mensaje.getContenidotexto());
DateFormat hourFormat = new SimpleDateFormat("HH:mm:ss");
hold.hora.setText(String.valueOf(hourFormat.format(mDataset.get(position).mensaje.getFecha())));
}
case ModelChat.COTIZACION_TYPE:{
System.out.println("Entre sin errores");
//HolderCotizacion hold = (HolderCotizacion) holder;
//hold.valor.setText("3000");
}
}
}
}
@Override
public int getItemViewType(int position) {
switch (mDataset.get(position).type) {
case 0:
return ModelChat.TEXTO_TYPE;
case 1:
return ModelChat.IMAGEN_TYPE;
case 2:
return ModelChat.COTIZACION_TYPE;
default:
return -1;
}
}
@Override
public int getItemCount() {
return mDataset.size();
}}
Model Chat Class
public class ModelChat {
public static final int TEXTO_TYPE=0;
public static final int IMAGEN_TYPE=1;
public static final int COTIZACION_TYPE=2;
public int type;
public Mensaje mensaje;
public Cotizacion cotizacion;
public ModelChat(int type, Mensaje mensaje) {
this.type = type;
this.mensaje = mensaje;
}
public ModelChat(int type, Cotizacion cotizacion) {
this.type = type;
this.cotizacion = cotizacion;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Mensaje getMensaje() {
return mensaje;
}
public void setMensaje(Mensaje mensaje) {
this.mensaje = mensaje;
}
public Cotizacion getCotizacion() {
return cotizacion;
}
public void setCotizacion(Cotizacion cotizacion) {
this.cotizacion = cotizacion;
}}
This is the layout of the text message
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutmensaje"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
android:gravity="left">
<android.support.v7.widget.CardView xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardViewMensaje"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:paddingBottom="200dp"
cardview:cardCornerRadius="10sp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<TextView
android:id="@+id/mensajetexto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hola meeen" />
<TextView
android:id="@+id/horamensaje"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Horamen" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView></LinearLayout>
And this is the quote Layout (It goes inside the RecyclerView)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutmensajecotizacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
android:gravity="left">
<android.support.v7.widget.CardView xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardViewMensaje"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:paddingBottom="200dp"
cardview:cardCornerRadius="10sp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textocotizacionmensaje"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Nueva Cotizacion"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/valorcotizacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Valor men" />
<TextView
android:id="@+id/horamensajecotizacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Horamen" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView></LinearLayout>
And this is the Chat Activity
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChatActivity">
<include layout="@layout/toolbarchat"
android:id="@+id/toolbarchat" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/imagenperfil"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nombreperfil"
android:text="Loperso"
android:layout_gravity="center"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/mensajes"
android:layout_width="match_parent"
android:layout_height="459dp"
android:layout_gravity="right"></android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/escribir"
android:hint="Meen, el mensaje"
android:layout_weight="1"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagenbuscar"
android:background="@android:drawable/ic_menu_gallery"
android:layout_gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/enviar"
android:hint="Envia men"/>
</LinearLayout>
</LinearLayout></android.support.constraint.ConstraintLayout>