【发布时间】:2014-08-20 10:30:32
【问题描述】:
目前我正在为我的应用程序处理消息传递页面。问题是我在列表视图中使用了两个不同的 xml。现在我可以让数据以正确的顺序显示在页面上,但是,当我滚动时,数据仍然以正确的顺序显示,但样式不同。例如,在滚动最后一条消息之前,如果我向上滚动则使用 messagesent.xml,然后向下滚动相同的消息现在具有 message.xml 样式。我目前使用的代码如下。
public class MessagingAdapter extends ArrayAdapter<MessagingObject> {
private static final String userID = "USERID";
private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
public String newid;
public Context context;
public ArrayList<MessagingObject> object = new ArrayList<MessagingObject>();
int type;
public MessagingAdapter(Context context, int textViewResourceId, ArrayList<MessagingObject> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.object = objects;
}
@Override
public int getItemViewType(int position) {
if (userID.equals(newid)) {
type = TYPE_ITEM1;
} else {
type = TYPE_ITEM2;
}
return type;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertview, ViewGroup parent) {
MessagingObject i = object.get(position);
newid = i.getMessageID();
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertview = inflater.inflate(R.layout.messagesright_layout, null);
if (convertview == null) {
convertview = inflater.inflate(R.layout.messagesright_layout, null);
}
if (type == TYPE_ITEM1) {
convertview = inflater.inflate(R.layout.messagesright_layout, null);
TextView sentmessage = (TextView) convertview.findViewById(R.id.messagesent_text);
sentmessage.setText(object.get(position).getMessage());
TextView senttime = (TextView) convertview.findViewById(R.id.messagesentTime);
String senttimestring = new String().valueOf(object.get(position).getTimeStamp());
senttime.setText(senttimestring);
} else {
convertview = inflater.inflate(R.layout.messages_layout, null);
TextView message = (TextView) convertview.findViewById(R.id.message_text);
message.setText(object.get(position).getMessage());
TextView time = (TextView) convertview.findViewById(R.id.messageTime);
String timestring = new String().valueOf(object.get(position).getTimeStamp());
time.setText(timestring);
}
return convertview;
}
现在我知道这与 Listview 回收有关,但是,我尝试了一些我在其他堆栈溢出帖子上找到的解决方案,但没有运气,而且我无法找到很多解决这个问题的方法2个xml。
感谢您的观看!
【问题讨论】:
标签: java android android-listview android-xml