好的;这是一个解决方案,可以为您提供构建的想法。它将解决您的日期问题,并提示您如何解决如何显示您的时间。这种程序需要大量工作才能使其作为应用程序可行。您需要考虑时区、存储消息的位置等。
现在堆栈溢出的目的不是为人们设计程序,而是试图帮助人们朝着正确的方向前进。
为了回答您的问题;我介绍的是基础知识,我不会给你准备好运行的代码,你需要做一些工作:)
这可以通过多种方式解决,我选择了以下方法:
使用共享偏好来存储最新的日期时间。我考虑访问聊天消息数组,但认为这可能更简单。我选择了共享首选项,以便在关闭和重新打开应用程序时保留最后一条消息的日期。 (最终聊天消息需要保留在数据库 [SQLite] 中,以便在关闭和打开应用程序时消息仍然存在)。请看我最后的评论。
public class ChatActivity extends ActionBarActivity {
public static final String MyPREF = "MyPrefs" ;
public static final String DATE_KEY = "DateKey" ;
SharedPreferences prefs;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
initControls();
pref = getSharedPreferences(MyPREF, MODE_PRIVATE);
// Check that the shared preferences has a value
if(contains(DATE_KEY)){
editor = prefs.edit();
// get the current datetime.
editor.Long(DATE_KEY, date);
editor.commit();
// set the text of the textview android:id="@+id/date"
// with the current formatted date.
}
}
在您的 onclick 监听器中,您测试最后存储的日期是今天的日期,如果不是,则将其转换为聊天消息,因此可以将其值添加到数组中并显示。您将不得不调整这些消息类型的格式,您甚至可以为这些日期消息添加一个唯一的 id 并对其进行测试,或者测试是否没有 id.. 有很多解决方案。
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/.../
}
ChatMessage chatMessage = new ChatMessage();
long LastDate = pref.getLong(DATE_KEY, date);
// create an empty chat message with only a date.
// check if the last used date is not today's date.
// if it's not today's date, save it and display it as
// an archived bubble.
if(!LastDate.isToday()){
ChatMessage dateholder = new ChatMessage();
// You'll need to format the date
dateholder.setDate(Formatted LastDate);
// set/change the text of the textview android:id="@+id/date"
// with the current formatted date.
}
// put the current date time into your preferences.
editor = prefs.edit();
editor.putString(DATE_KEY, date);
editor.commit();
/.../
// this is now setting up the new chat message.
chatMessage.setDate(DateFormat.getDateTimeInstance().format(new Date()));
/.../
displayMessage(chatMessage);
}
});
xml 好的,现在您不希望日期在只有一条消息时浮动在屏幕顶部。因此,将它与滚动列表分组,然后将其设置为相对于屏幕布局,因为列表本身就是。线性布局可以做到这一点,只要确保它的方向设置为垂直。
将它们固定在一起。
<LinearLayout
android:layout_above="@+id/messageEdit"
android:layout_below="@+id/meLbl"
android:id="@+id/layout"
.../orentation:vertical ../
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
/..//>
<ListView
android:id="@+id/messagesContainer"
.../
...//>
</LinearLayout>
<TextView
android:id="@+id/meLbl"
/.../
/>
<TextView
android:id="@+id/friendLabel"
/.../
/>
更改每条消息的显示您需要停止显示每条消息的日期时间。如果您希望单独显示每条消息的时间,并在同一分钟内跳过这些消息,正如您在 cmets 中提到的,那么我已经为您提供了框架,以制定如何测试这一点以及程序逻辑是否或不显示。
你需要在你的:
public View getView(final int position, View convertView, ViewGroup parent) {
holder.txtMessage.setText(chatMessage.getMessage());
// TODO,
// do a check if(mins are the same don't post
// else post the format without the date)
// holder.txtInfo.setText(chatMessage.getDate());
不要忘记应用所有必要的导入。
我建议您是否开始使用您从那里访问最后日期的数据库而没有共享首选项,或者继续使用共享首选项。这变得更加复杂,因为要保存多个聊天,在这种情况下使用数据库会更好。