0

问题:我正在尝试使用自定义ListView在聊天中显示消息。我已经ArrayList用我自己的类的对象参数化了,代码如下。

问题:每次布局膨胀时,消息文本和时间戳始终保持不变,getView()方法有问题,但我不知道是什么。

PS ChatActivity 包含大量代码,我只会发布与问题相关的内容以保持其干净。

消息.java:

 public class Message{

/*
 * 1- outgoing
 * 2-incoming
 * */

public String message;
public String timestamp;
public TextView tvmessage, tvtimestamp;
public ImageView indicator;
public int type;

public Message(String text, int type){
    this.timestamp = generateTimestamp();
    this.message = text;
    this.type=type;

    Log.d("Message", message);
    //Message text is set correctly each time the constructor is called

}

@SuppressLint("SimpleDateFormat")
public String generateTimestamp() {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    String ts = sdf.format(new Date());

    return ts;
}

    }

聊天活动:

 private ArrayList<Message> OutgoingMessagesList;
 private SingleChatAdapter adapter;
  private EditText etInput;
 lv = getListView();
    adapter = new SingleChatAdapter(this, OutgoingMessagesList);
    lv.setAdapter(adapter);

public void sendMessage() {

    String tmpMessage = etInput.getText().toString().trim();
    /*--- check whether the message string is empty ---*/
    if (tmpMessage.length() > 0) {

        Message msg = new Message(tmpMessage, 1);

        OutgoingMessagesList.add(msg);

        /*
         * TODO: save the message to local database, send the message to
         * server, save it to server database and forward to the recipient
         */
        adapter.notifyDataSetChanged();
        etInput.setText("");

    } else {

        MiscUtils.gibShortToast(this, getString(R.string.msgEmpty));
    }

}

我认为问题所在的适配器类是:

    public final class SingleChatAdapter extends BaseAdapter {
private Context context;
Message msg;
private ArrayList<Message> messages;

public SingleChatAdapter(Context context, ArrayList<Message> chat) {

    this.context = context;
    this.messages = chat;
    Log.d("ChatAdapter", "called constructor");

}

public int getCount() {
    return messages.size();
}

public Object getItem(int position) {
    return messages.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup viewGroup) {

    msg = (Message) getItem(position);

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // TODO message_incoming should be inflated for messages received
        // from server
        convertView = (LinearLayout) inflater.inflate(
                R.layout.message_outgoing, null);

        convertView.setTag(msg);

    } else {

        msg = (Message) convertView.getTag();

    }

    msg.tvmessage = (TextView) convertView
            .findViewById(R.id.tvMessageOutgoing);
    msg.tvtimestamp = (TextView) convertView.findViewById(R.id.tvTimestamp);
    msg.indicator = (ImageView) convertView
            .findViewById(R.id.imgMessageState);

    msg.tvmessage.setText(msg.message);

    msg.tvtimestamp.setText(msg.timestamp);

    return convertView;
}

我的 Message 类中的消息字符串每次都包含正确的值,但膨胀到列表中的文本始终保持不变。我错过了什么?

4

2 回答 2

3

您应该删除 else 语句(getView 中的那个),因为现在它以这种方式工作:如果 convertView 为 null(创建 listView 的第一项),那么它会被夸大,并且您将消息设置为该视图的标签。但是当创建下一个项目时,convertView 不再为空,并且您的 else 语句被调用。因此,您的消息将替换为保存为 convertView 的标签的消息。

您还应该观看孔子提到的视频:)

于 2013-02-03T13:35:16.863 回答
2

如果你想了解 ListView 是如何工作的,getView请参考Roman Guy 的这篇演讲,这里也是一篇不错的博文

于 2013-02-03T13:19:20.473 回答