android - 如何向适配器添加时间戳
问题描述
我正在尝试制作一个聊天气泡,我需要在每个气泡中插入时间戳,我有我的适配器来插入消息,但我不知道如何添加时间戳,有人可以告诉我该怎么做。
这里的 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;
}
}
解决方案
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);
}
- 更新帮助方法以返回格式化的日期字符串:
private String readFechaActual(long timestamp){
return simpleDateFormat.format(new Date(timestamp));
}
}
- 使用它
getView
:
//set message content
holder.msg.setText(ChatBubble.getContent());
holder.dateTimeTx.setText("" + ChatBubble.getTimestamp()); // <--- ADD HERE
- 适配器只连接视图和模型,但实际数据保留在模型中,因此您应该更改
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;
}
}
推荐阅读
- r - 所需响应中没有数据时的 HTTR 警告
- python-3.x - 在python中将文件分块为可变大小的读取
- java - 为什么我的程序打印值没有相应的打印语句?
- flutter - 在颤动中,当按下活动标签栏按钮时,有一种方法可以重建当前标签
- azure-devops - 如何设置 Azure DevOps 管道,以便在我推送到 BitBucket 存储库时触发?
- python - 如何使用 Kivy 和 Python 使我的点“ping”?
- android - 无法解析 Android 3.6.1 中的符号内容
- python - Astropy:将 FITS 表拆分为训练和测试集
- javascript - JavaScript array.includes 对象的奇怪行为
- python - 使用 scrapy 进行网页抓取:空结果