首页 > 解决方案 > 如何向适配器添加时间戳

问题描述

我正在尝试制作一个聊天气泡,我需要在每个气泡中插入时间戳,我有我的适配器来插入消息,但我不知道如何添加时间戳,有人可以告诉我该怎么做。

这里的 MainActivity 代码:

public class MainActivity extends AppCompatActivity {

private ListView listView;
private View btnSend;
private EditText editText;
boolean myMessage = true;
private List<ChatBubble> ChatBubbles;
private ArrayAdapter<ChatBubble> adapter;

private TextView dateTimeTx;
private TextView dateTimeRx;
private String dateTime;
private String timestamp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ChatBubbles = new ArrayList<>();

    listView = (ListView) findViewById(R.id.list_msg);
    btnSend = findViewById(R.id.btn_chat_send);
    editText = (EditText) findViewById(R.id.msg_type);

    //set ListView adapter first
    adapter = new MessageAdapter(this, R.layout.tx_chat_bubble, ChatBubbles);
    listView.setAdapter(adapter);

    //event for button SEND
    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (editText.getText().toString().trim().equals("")) {
                Toast.makeText(MainActivity.this, "Please input some text...", Toast.LENGTH_SHORT).show();
            } else {
                //add message to list

                ChatBubble ChatBubble = new ChatBubble(editText.getText().toString(), myMessage, timestamp.toString() );

                ChatBubbles.add(ChatBubble);
                adapter.notifyDataSetChanged();
                editText.setText("");
                if (myMessage) {
                    myMessage = false;
                } else {
                    myMessage = true;
                }
            }
        }
    });
 }
}

但我有一个错误:

ChatBubble ChatBubble = new ChatBubble(editText.getText().toString(), myMessage, timestamp.toString() );

这里mi适配器代码:

public class MessageAdapter extends ArrayAdapter<ChatBubble> {
private Activity activity;
private List<ChatBubble> messages;
private String dateTime;
private SimpleDateFormat simpleDateFormat ;

public MessageAdapter(Activity context, int resource, List<ChatBubble> objects) {
    super(context, resource, objects);
    this.activity = context;
    this.messages = objects;

    String isoDatePattern = "dd/MM/yyyy  HH:mm";
    simpleDateFormat = new SimpleDateFormat(isoDatePattern);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    int layoutResource = 0; // determined by view type
    ChatBubble ChatBubble = getItem(position);
    int viewType = getItemViewType(position);

    if (ChatBubble.myMessage()) {
        layoutResource = R.layout.tx_chat_bubble;
    } else {
        layoutResource = R.layout.rx_chat_bubble;
    }

    if (convertView != null) {
        holder = (ViewHolder) convertView.getTag();
    } else {
        convertView = inflater.inflate(layoutResource, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    }
    //set message content
    holder.msg.setText(ChatBubble.getContent());
    holder.dateTimeTx.setText(ChatBubble.getTimestamp());
    return convertView;
}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return position % 2;
}

private class ViewHolder {
    private TextView msg;
    private TextView dateTimeTx;
    public ViewHolder(View v) {
        msg = (TextView) v.findViewById(R.id.txt_msg);
        dateTimeTx = (TextView) v.findViewById(R.id.time_msg);
    }
}

private String readFechaActual(long timestamp){
    return simpleDateFormat.format(new Date(timestamp));
 }
}

但我有另一个错误:

holder.dateTimeTx.setText(ChatBubble.getTimestamp());

这是 ChatBubble 类:

public class ChatBubble {

private String content;
private boolean myMessage;
private long timestamp;

public ChatBubble(String content, boolean myMessage, long timestamp) {
    this.content = content;
    this.myMessage = myMessage;
    this.timestamp = timestamp;
}

public String getContent() {
    return content;
}

public boolean myMessage() {
     return myMessage;
}

public long getTimestamp() {
    return timestamp;
 }
}

标签: android

解决方案


  1. SimpleDateFormat在适配器构造函数中创建单个(以避免未使用的资源需求) :
private SimpleDateFormat simpleDateFormat ;

public MessageAdapter(Activity context, int resource, List<ChatBubble> objects) {
    super(context, resource, objects);
    this.activity = context;
    this.messages = objects;

    String isoDatePattern = "dd/MM/yyyy  HH:mm";
    simpleDateFormat = new SimpleDateFormat(isoDatePattern);
}
  1. 更新帮助方法以返回格式化的日期字符串:
private String readFechaActual(long timestamp){
    return simpleDateFormat.format(new Date(timestamp));
  }
}
  1. 使用它getView
    //set message content
    holder.msg.setText(ChatBubble.getContent());
    holder.dateTimeTx.setText("" + ChatBubble.getTimestamp());  // <--- ADD HERE
  1. 适配器只连接视图和模型,但实际数据保留在模型中,因此您应该更改BubbleChat类并添加时间戳字段:
public class ChatBubble {

private String content;
private boolean myMessage;
private long timestamp;

public ChatBubble(String content, boolean myMessage, long timestamp) {
    this.content = content;
    this.myMessage = myMessage;
    this.timestamp = timestamp;
}

public String getContent() {
    return content;
}

public boolean myMessage() {
    return myMessage;
 }

public long getTimestamp() {
  return timestamp;
}
}

推荐阅读