我做了一個ListView
,我想根據iMsg
和uMsg
(它們中的任何一個總是空白,所以另一個應該被誇大)的值膨脹不同的xml文件。TypeHolder中的ClassCastException Android
我有2個xml文件。兩者都有TextView
不同的造型。該代碼投了一個ClassCastException
。我讀了另一個問題的答案,以覆蓋getViewTypeCount()
方法。但我不太明白。誰能幫忙?
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;
}
}
下面是日誌:
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)
如果你可以在你的問題中追加異常堆棧跟蹤,這將有所幫助。 –