在我的开发中,我试图将three different views of rows
放入数组列表中,为此,我正在使用方法getViewTypeCount
和getItemViewType
,但是由于某些原因,我无法使其正常工作,当我将相同的代码仅用于two types of row views
时,它就可以正常工作。
我正在使用变量String myMessage
选择要显示的视图
有人可以告诉我我的错误是什么吗?
在这里上课:
public class MessageAdapter extends ArrayAdapter<ChatBubble> {
private Activity activity;
private List<ChatBubble> messages;
private int state;
public MessageAdapter(Activity context, int resource, List<ChatBubble> objects) {
super(context, resource, objects);
this.activity = context;
this.messages = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
int layoutResource = 0; // determined by view type
ChatBubble ChatBubble = getItem(position);
int viewType = getItemViewType(position);
switch(ChatBubble.myMessage()){
case "0":
layoutResource = R.layout.date_chat_bubble;
break;
case "1":
layoutResource = R.layout.tx_chat_bubble;
break;
case "2":
layoutResource = R.layout.rx_chat_bubble;
break;
}
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
convertView = inflater.inflate(layoutResource, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
//set message content
holder.nameRx.setText(ChatBubble.getISSDD());
holder.msg.setText(ChatBubble.getContent());
holder.stringTime.setText( ChatBubble.getStringTime());
//holder.stringDate.setText( ChatBubble.getStringDate());
return convertView;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position) {
ChatBubble chatBubble = messages.get(position);
switch (chatBubble.myMessage()){
case "0":
state = 0;
break;
case "1":
state = 1;
break;
case "2":
state = 2;
break;
}
return state;
}
private class ViewHolder {
private TextView msg;
private TextView stringTime;
private TextView stringDate;
private TextView nameRx;
public ViewHolder(View v) {
msg = (TextView) v.findViewById(R.id.txt_msg);
stringTime = (TextView) v.findViewById(R.id.time_msg);
stringDate = (TextView) v.findViewById(R.id.date_msg);
nameRx = (TextView) v.findViewById(R.id.name_rx);
}
}
}
这里是ChatBubble类:
public class ChatBubble {
private String content;
private String myMessage;
private String stringTime;
private String stringDate;
private String iSsDd;
public ChatBubble(String content, String myMessage, String stringTime, String stringDate, String iSsDd) {
this.content = content;
this.myMessage = myMessage;
this.stringTime = stringTime;
this.stringDate = stringDate;
this.iSsDd = iSsDd;
}
public String getContent() {
return content;
}
public String myMessage() {
return myMessage;
}
public String getStringTime() { return stringTime;}
public String getStringDate() { return stringDate;}
public String getISSDD() { return iSsDd;}
}