Android Viewholder Classcastexception

Asked

Viewed 10 times

0

I implemented a message listing for a chat. The Sentry is receiving an exception that occurs in production, I have tried to simulate, but without success.

Error

ClassCastException
java.lang.Integer cannot be cast to com.app.ui.chat.adapter.ConversaAdapter$ViewHolder
com.app.ui.chat.adapter.ConversaAdapter in getView at line 83
Called from: android.widget.AbsListView in obtainView

Conversaadapter.java

package com.app.ui.chat.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.R;
import com.app.data.network.model.ChatItemModel;

import com.app.ui.conversa.ConversaActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ConversaAdapter extends BaseAdapter {

    Activity act;
    ArrayList<ChatItemModel> items;

    class ViewHolder {
        TextView txtTitulo;
        TextView txtNaoLido;
        TextView txtSubtitulo;
        TextView txtDataHora;
        TextView txtCorpoMensagem;
        LinearLayout lnlCardChat;


        ViewHolder(View v) {
            txtTitulo = v.findViewById(R.id.txtTitulo);
            txtNaoLido = v.findViewById(R.id.txtNaoLido);
            txtSubtitulo = v.findViewById(R.id.txtSubtitulo);
            txtDataHora = v.findViewById(R.id.txtDataHora);
            txtCorpoMensagem = v.findViewById(R.id.txtCorpoMensagem);
            lnlCardChat = v.findViewById(R.id.lnlCardChat);
        }
    }

    public ConversaAdapter(Activity act, ArrayList<ChatItemModel> items) {
        this.act = act;
        this.items = items;
    }

    @Override
    public int getCount() {
        return this.items.size();
    }

    @Override
    public ChatItemModel getItem(int position) {
        return this.items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;

        if(convertView == null) {
            view = act.getLayoutInflater().inflate(R.layout.adapter_chat_list, parent, false);

            holder = new ViewHolder(view);

            view.setTag(holder);


            holder.lnlCardChat.setOnClickListener(v -> {
                ChatItemModel model = getItem((int) v.getTag());
                act.startActivity(ConversaActivity.getStartIntent(act, model));
            });

        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        holder.lnlCardChat.setTag(position);

        ChatItemModel chatItemModel = getItem(position);
        holder.txtTitulo.setText(chatItemModel.getTitle());

        if(chatItemModel.getLastMessage() != null) {
            try {
                SimpleDateFormat fromDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date time = fromDateFormat.parse(chatItemModel.getLastMessage().getCreatedAt());
                SimpleDateFormat toDateFormat = new SimpleDateFormat("dd/MM HH:mm");

                holder.txtDataHora.setText(toDateFormat.format(time) + " ");
            } catch (Exception ignore) {
            }
            holder.txtCorpoMensagem.setText(chatItemModel.getLastMessage().getVcMessage());
        } else {
            holder.txtDataHora.setText("");
            holder.txtCorpoMensagem.setText("");
        }
        holder.txtSubtitulo.setText(chatItemModel.getSubtitle());

        holder.txtNaoLido.setVisibility(chatItemModel.isReaded() ? View.GONE : View.VISIBLE);
        if(!chatItemModel.isReaded()) {
            holder.txtDataHora.setTextColor(act.getColor(R.color.md_blue_600));
        } else {
            holder.txtDataHora.setTextColor(act.getColor(R.color.md_black_1000));
        }

        return view;
    }
}

The exception occurs when I do this: holder = (ViewHolder) view.getTag();

Can anyone tell me where I’m going wrong?

No answers

Browser other questions tagged

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