我使用自定义适配器,Message类等...我无法更新第一个ListItem,第二个和其他正确添加在我的Listview中。通过更新我的意思是我默认使用textView的文本名称,而不是消息!
你可以帮我理解为什么吗?谢谢! 这是代码的一部分: (listView在对话框窗口中) // (variables put in public static)
chat_dialogue = new Dialog(context, R.style.hidetitle);
chat_dialogue.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
chat_dialogue.setContentView(R.layout.wakeupcalls); // show the dialogue
edit_Text_Chat = (EditText) chat_dialogue.findViewById(R.id.editText_chat);
chat_listView = (ListView)chat_dialogue.findViewById(R.id.ListViewChat);
Fsa.messages = new ArrayList<Message>();
Fsa.messages.add(new Message(the_sender,the_message,"(" + Fsa.date + Fsa.pre_mess2 + Fsa.heure + ")",true));
adapter = new bubbleadapter(context,messages);
chat_listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
chat_listView.setSelection(messages.size()-1);
然后我从其他方法中添加一些新的消息,它们显示完美,但不是上面的这个! (正如我所说,这只是第一个!)
现在是自定义适配器:
public class bubbleadapter extends BaseAdapter{
private Context mContext;
private ArrayList<Message> mMessages;
public bubbleadapter(Context context, ArrayList<Message> messages){
super();
this.mContext = context;
this.mMessages = messages;
}
@Override
public int getCount(){
return mMessages.size();
}
@Override
public Object getItem(int position) {
return mMessages.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
Message message = (Message) this.getItem(position);
ViewHolder holder;
if (convertView ==null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.chat_row, parent,false);
holder.sender = (TextView) convertView.findViewById(R.id.textVSender);
holder.message = (TextView) convertView.findViewById(R.id.message_text1);
holder.date = (TextView) convertView.findViewById(R.id.textVDate);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.sender.setText(message.getSender());
holder.message.setText(message.getMessage());
holder.date.setText(message.getDate());
LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();
if (message.isStatusMessage()) {
holder.message.setBackgroundDrawable(null);
holder.sender.setText(message.getSender());
holder.sender.setTextColor(Color.parseColor("#405478"));
} else {
if (message.isMine()) {
holder.message.setBackgroundResource(R.drawable.speech_bubble_green);
lp.gravity = Gravity.RIGHT;
} else {
holder.message.setBackgroundResource(R.drawable.speech_bubble_orange);
lp.gravity = Gravity.LEFT;
}
holder.message.setLayoutParams(lp);
}
}
return convertView;
}
private static class ViewHolder {
TextView sender;
TextView message;
TextView date;
}
@Override
public long getItemId(int position) {
return position;
}
}
答案 0 :(得分:0)
删除else语句如下:
public View getView(int position, View convertView, ViewGroup parent) {
Message message = (Message) this.getItem(position);
ViewHolder holder;
if (convertView ==null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.chat_row, parent,false);
holder.sender = (TextView) convertView.findViewById(R.id.textVSender);
holder.message = (TextView) convertView.findViewById(R.id.message_text1);
holder.date = (TextView) convertView.findViewById(R.id.textVDate);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.sender.setText(message.getSender());
holder.message.setText(message.getMessage());
holder.date.setText(message.getDate());
LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();
if (message.isStatusMessage()) {
holder.message.setBackgroundDrawable(null);
holder.sender.setText(message.getSender());
holder.sender.setTextColor(Color.parseColor("#405478"));
} else {
if (message.isMine()) {
holder.message.setBackgroundResource(R.drawable.speech_bubble_green);
lp.gravity = Gravity.RIGHT;
} else {
holder.message.setBackgroundResource(R.drawable.speech_bubble_orange);
lp.gravity = Gravity.LEFT;
}
holder.message.setLayoutParams(lp);
}
return convertView;
}