gpt4 book ai didi

java - 如何在群聊中显示消息和时间?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:02:47 25 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,当我在群聊中发送消息时,我有一个群聊功能,我需要显示消息和时间(在消息的末尾,时间需要显示在每个聊天消息)。

Here is my layout code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingLeft="10dp">

<TextView
android:id="@+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="30sp"
android:layout_gravity="left"
android:textSize="12sp"
android:textColor="@color/lightred"
android:textStyle="italic"
/>

<TextView
android:id="@+id/txtMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_marginRight="80dp"
android:background="@color/grey"
android:textColor="@color/white"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textStyle="italic|bold"
/>

</LinearLayout>

"@+id/txtInfo"用于获取当前日期和时间 & "@+id/txtMsg"用于显示消息。

现在我的输出看起来像这样 enter image description here

但我期待我的输出, enter image description here

我的编程代码如下,

 public class MessagesListAdapter extends BaseAdapter {

private Context context;
private List<ChatMessageObjects> messagesItems;

public MessagesListAdapter(Context context, List<ChatMessageObjects> navDrawerItems) {
this.context = context;
this.messagesItems = navDrawerItems;
}

@Override
public int getCount() {
return messagesItems.size();
}

@Override
public Object getItem(int position) {
return messagesItems.get(position);
}

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


@Override
public View getView(int position, View convertView, ViewGroup parent) {

/**
* The following list not implemented reusable list items as list items
* are showing incorrect data Add the solution if you have one
* */

ChatMessageObjects m = messagesItems.get(position);

LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

if (messagesItems.get(position).getMessage_type() == Constants.IS_TYPE_CHAT_IMAGE) {
convertView = mInflater.inflate(R.layout.chat_image,
null);

ImageView imageView = (ImageView) convertView.findViewById(R.id.imgView);
TextView imageLabel = (TextView) convertView.findViewById(R.id.lblImage);
if (messagesItems.get(position).isSelf() == 0) {
Log.i(Constants.TAG, " the value is from others");
try {
URL url = new URL(messagesItems.get(position).getMessage());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
imageView.setImageBitmap(BitmapFactory.decodeStream(input));
} catch (IOException e) {
Log.i(Constants.TAG, e.toString());
}
} else if (messagesItems.get(position).isSelf() == 1) {
Log.i(Constants.TAG, " the value is itself");
imageView.setImageURI(Uri.fromFile(imgFile));
imageLabel.setText(messagesItems.get(position).getFromName());
}
} else if (messagesItems.get(position).getMessage_type() == Constants.MESSAGE_TYPE_MSG) {
if (messagesItems.get(position).isSelf() == 1) {
// message belongs to you, so load the right aligned layout
convertView = mInflater.inflate(R.layout.chat_message_right,
null);
TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
//date and time added here
TextView date = (TextView) convertView.findViewById(R.id.txtInfo);
txtMsg.setText(m.getMessage());
lblFrom.setText(m.getFromName());
date.setText(m.getDate());
} else if (messagesItems.get(position).isSelf() == 0) {
// message belongs to other person, load the left aligned layout
convertView = mInflater.inflate(R.layout.chat_message_left,
null);
TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
//date and time added here
TextView date = (TextView) convertView.findViewById(R.id.txtInfo);
txtMsg.setText(m.getMessage());
lblFrom.setText(m.getFromName());
date.setText(m.getDate());
}

}

return convertView;
}

日期.setText(m.getDate());代码以这种格式“28-aug-2015 11:46:11pm”获取我的日期。我需要将我的日期转换为这种格式“11:46 pm”,我该如何更改这种格式以及如何显示聊天消息以及时间,如上所示,请帮助我。

最佳答案

只需将您的 LinearLayout 方向从“垂直”更改为“水平”。

您可以使用 Time 来表示小时和分钟,如下所示:

    Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
String timeInput = today.hour + ":" + today.minute;

更好的方法是使用 SimpleDateFormat:

    SimpleDateFormat s = new SimpleDateFormat("HH:mm a");
String msgTime = s.format(new Date());

编辑:

Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
String msgTime = today.format("HH:mm a")

使用此示例显示格式化的时间。

关于java - 如何在群聊中显示消息和时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32264714/

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