androidclasscastexceptionconvertview

ClassCastException in TypeHolder Android


I have made a ListView and I want to inflate different xml file depending on the value of iMsg and uMsg (either one of them will always be blank, so the other should be inflated).

There are 2 xml files I am having. Both having a TextView with different styling. The code casts a ClassCastException. I read in another question's answer to override the getViewTypeCount() method. But I didn't quite get it. Can anyone help?

public class ChatAdapter extends ArrayAdapter<Chatmsg>{
    public ChatAdapter(Context context, ArrayList<Chatmsg> messages) {
        super(context,0,messages);
    }

    public static class iViewHolder{
        TextView imsg;
    }
    public static class uViewHolder{
        TextView umsg;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        Chatmsg chatmsg = getItem(position);
        View v = null;

        if(chatmsg.getuMsg()== null) {
            iViewHolder iviewHolder;
            v = convertView;

            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.pc_custom_row_i, parent, false);

                iviewHolder = new iViewHolder();
                iviewHolder.imsg = (TextView) v.findViewById(R.id.iMessage);
                v.setTag(iviewHolder);
            } else {
                iviewHolder = (iViewHolder) v.getTag();
            }

            iviewHolder.imsg.setText(chatmsg.getiMsg());

        } else {
                uViewHolder uviewHolder;
                v = convertView;

                if(v== null){
                    LayoutInflater vi1 = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi1.inflate(R.layout.pc_custom_row_u,parent,false);
                    uviewHolder = new uViewHolder();
                    uviewHolder.umsg = (TextView) v.findViewById(R.id.uMessage);
                    v.setTag(uviewHolder);

                } else {
                    uviewHolder = (uViewHolder) v.getTag();
                }

                uviewHolder.umsg.setText(chatmsg.getuMsg());
        }

        return v;
    }
}

Here is the Log:

E/AndroidRuntime:   
FATAL EXCEPTION: main Process: in.techxator.zero.zero_chat, PID: 28876  
java.lang.ClassCastException:in.techxator.zero.zero_chat.ChatAdapter$iViewHolder cannot be cast to in.techxator.zero.zero_chat.ChatAdapter$uViewHolder at in.techxator.zero.zero_chat.ChatAdapter.getView(ChatAdapter.java:120)

Solution

  • I edited my previous answer as it was not an answer. I saw the question is about ListView but anyway, as I'm working mostly with RecyclerView these days, I came up with a solution for RecyclerView.

    Here's you need to override the getViewTypeCount method. The Android system doesn't know that there are different view types if you don't define them.

    You can have a look at this answer too. The answer itself doesn't override the getViewTypeCount. You need to implement this method explicitly and return 2 as you have two different ViewHolder.

    You can take a look at this answer too which implements a custom adapter with getViewTypeCount to get an idea.