定义ListView中的 多布局时, 报错 数组角标越界,可就是不显示错误在哪。
找了好久 终于让我解决了! 在
就在这一句 return TYPE_SIZE时 要记住 这个size要大于你布局的数,比如你你有三个布局,这里就要写4或大于4,不然报错,错哪你都不知道!!
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return TYPE_SIZE;
}
ListView 多布局 显示 如下:
class ChatListAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return chatMsgList.size();
}
@Override
public ChatMsg getItem(int position) {
// TODO Auto-generated method stub
return chatMsgList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getItemViewType(int position) {
return chatMsgList.get(position).getType();
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return TYPE_SIZE;//size要大于布局个数
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
int type = getItemViewType(position);
if(convertView == null){
switch (type) {
case SEND_MSG://该数值 不能大于 TYPE_SIZE 否则一样报错!
convertView = inflater.inflate(R.layout.chat_me,null);
holder = new Holder();
holder.msg = (TextView) convertView.findViewById(R.id.msg_me);
convertView.setTag(holder);
LogManager.i(TAG, "chat_me");
break;
case RECEIVER_MSG:
convertView = inflater.inflate( R.layout.chat_other, null);
holder = new Holder();
holder.msg = (TextView) convertView.findViewById(R.id.msg_other);
holder.icon = (Button) convertView.findViewById(R.id.header_icon);
convertView.setTag(holder);
LogManager.i(TAG, "chat_other");
break;
}
}else{
holder = (Holder) convertView.getTag();
}
ChatMsg chatMsg = chatMsgList.get(position);
switch (type) {
case SEND_MSG:
holder.msg.setText(chatMsg.getMsg());
break;
case RECEIVER_MSG:
holder.msg.setText(chatMsg.getMsg());
holder.icon.setBackgroundResource(R.drawable.h091);
break;
}
return convertView;
}
class Holder{
TextView msg;
Button icon;
}
}
版权声明:本文为a231930原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。