gpt4 book ai didi

java - 无法使用自定义适配器设置第一个 ListView 项目

转载 作者:太空宇宙 更新时间:2023-11-04 15:04:48 25 4
gpt4 key购买 nike

我使用自定义适配器、消息类等...我无法更新第一个 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;
}

}

最佳答案

删除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;
}

关于java - 无法使用自定义适配器设置第一个 ListView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22147419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com