我的自定义适配器包含一个getView方法,并且getView的位置没有正确递增。副作用是所有值都显示在两个布局中(row_chat_me和amp_chat_others)。 IsMsgFromMe正确识别消息作者是谁(message.GetFrom())。 chatMsgList是一个包含Message对象的ArrayList。
谢谢!
public class ChatAdapter extends ArrayAdapter<Message> {
public ChatAdapter(Context context, ArrayList<Message> messages) {
super(context, 0, messages);
}
public boolean IsMsgFromMe(Message message) {
boolean isSenderMe = ChatFragment.username.equals(message.GetFrom());
return isSenderMe;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Get the data item for this position
Message message = ChatFragment.chatMsgList.get(position);
if (convertView == null) {
holder = new ViewHolder();
//Check who has sent the message me or someone else...
if (IsMsgFromMe(message)) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_chat_me, parent, false);
holder.chatFrom = (TextView) convertView.findViewById(R.id.txt_message_me_from);
holder.chatMessage = (TextView) convertView.findViewById(R.id.txt_message_me_message);
holder.chatTime = (TextView) convertView.findViewById(R.id.txt_message_me_time);
} else {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_chat_others, parent, false);
holder.chatFrom = (TextView) convertView.findViewById(R.id.txt_message_others_from);
holder.chatMessage = (TextView) convertView.findViewById(R.id.txt_message_others_message);
holder.chatTime = (TextView) convertView.findViewById(R.id.txt_message_others_time);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
holder.chatFrom.setText("From: " + message.GetFrom());
holder.chatMessage.setText(message.GetMsg());
holder.chatTime.setText("Date: " + message.GetTime());
// Return the completed view to render on screen
return convertView;
}
public static class ViewHolder {
public TextView chatFrom;
public TextView chatMessage;
public TextView chatTime;
}
}
答案 0 :(得分:0)
我的建议是,您使用传递到适配器的消息列表,而不是引用ChatFragment
中的列表。所以我会像这样修改适配器代码:
public class ChatAdapter extends ArrayAdapter<Message> {
private ArrayList<Message> messages;
public ChatAdapter(Context context, ArrayList<Message> messages) {
super(context, 0, messages);
this.messages = messages;
}
...
除非您对2个聊天行有不同的背景,或者在2个聊天行之间存在其他一些主要差异,否则我认为您只需要一行布局。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Get the data item for this position
Message message = messages.get(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_chat, parent, false);
holder.chatFrom = (TextView) convertView.findViewById(R.id.txt_message_from);
holder.chatMessage = (TextView) convertView.findViewById(R.id.txt_message_message);
holder.chatTime = (TextView) convertView.findViewById(R.id.txt_message_time);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
holder.chatFrom.setText("From: " + message.GetFrom());
holder.chatMessage.setText(message.GetMsg());
holder.chatTime.setText("Date: " + message.GetTime());
// Return the completed view to render on screen
return convertView;
}
单行布局示例:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_message_time"
android:layout_width="fill_parent"
android:layout_height="25dp"/>
<TextView
android:id="@+id/txt_message_from"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:layout_below="@+id/txt_message_time" />
<TextView
android:id="@+id/txt_message_message"
android:layout_width="fill_parent"
android:layout_below="@+id/txt_message_from"
android:layout_height="50dp" />
</RelativeLayout>
我希望这会有所帮助。
答案 1 :(得分:0)
如上面的代码,在getView()中,当convertView!= null时,可能convertView从R.layout.row_chat_me而不是R.layout.row_chat_others(和。
因此,您应该覆盖 getItemType()和 getViewTypeCount(),以在适配器中定义2(或更多)项目视图类型。
您可以在此处阅读更多详细信息:http://android.amberfog.com/?p=296(部分不同列表项的布局)