我有一个来自我的两个用户的消息列表,并希望将这些消息显示为两个不同颜色和定位的聊天气泡。使用我当前的代码,我的所有消息都会毫无问题地填充到列表视图中,只有颜色和定位无法正常工作。
在我的Activity中,我有以下代码在onCreate()上调用:
public void populateMessages() {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String currentUser = sharedPrefs.getString("Username", null);
DatabaseReference mRef = FirebaseDatabase.getInstance().getReferenceFromUrl(Passwords.FB_URL).child("Message").child(currentUser).child(binderContact.getNumber());
time = System.currentTimeMillis();
Query queryRef = mRef.orderByChild("time").startAt(time);
adapter = new MessageAdapter(this, Message.class, R.layout.message_list_row, queryRef);
ListView lv = (ListView) findViewById(R.id.list_view_chalkboard);
lv.setAdapter(adapter);
}
这是我的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<ImageView
android:id="@+id/message_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_alarm_grey600_18dp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/message"
android:layout_alignStart="@+id/message"
/>
<TextView
android:id="@+id/message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="YESTERDAY"
android:textSize="12sp"
android:layout_alignBottom="@+id/message_type"
android:layout_toRightOf="@+id/message_type"
android:layout_toEndOf="@+id/message_type"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chat_bubble2"
android:gravity="center"
android:text="Test"
android:textIsSelectable="false"
android:textSize="18sp"
android:layout_below="@+id/message_time"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
这是我的FirebaseListAdapter:
public class MessageAdapter extends FirebaseListAdapter<Message> {
private String currentUser;
RelativeLayout.LayoutParams params;
public MessageAdapter(Activity activity, Class<Message> messageClass, int modelLayout, Query ref) {
super(activity, messageClass, modelLayout, ref);
}
@Override
protected void populateView(View view, Message m, int i) {
TextView messageTime = (TextView)view.findViewById(R.id.message_time);
long time = m.getTime();
TimeZone tz = TimeZone.getDefault();
Calendar now = Calendar.getInstance();
Calendar msgTime = Calendar.getInstance();
msgTime.setTimeInMillis(time);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(view.getContext());
currentUser = sharedPrefs.getString("Username", null);
TextView msg = (TextView)view.findViewById(R.id.message);
if (m.getFrom() == currentUser) {
msg.setBackgroundResource(R.drawable.chat_bubble2);
params = (RelativeLayout.LayoutParams)msg.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_END);
}
else {
msg.setBackgroundResource(R.drawable.chat_bubble);
params = (RelativeLayout.LayoutParams)msg.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
}
msg.setLayoutParams(params);
msg.setText(m.getText());
if (now.get(Calendar.DATE) == msgTime.get(Calendar.DATE)) {
Date date = new Date(time);
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
dateFormat.setTimeZone(tz);
String formattedDate = mActivity.getString(R.string.today) + dateFormat.format(date);
messageTime.setText(formattedDate);
}
else if (msgTime.get(Calendar.DATE) - now.get(Calendar.DATE) == 1){
Date date = new Date(time);
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
dateFormat.setTimeZone(tz);
String formattedDate = mActivity.getString(R.string.tomorrow) + dateFormat.format(date);
messageTime.setText(formattedDate);
}
else {
Date date = new Date(time);
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
dateFormat.setTimeZone(tz);
String formattedDate = dateFormat.format(date);
messageTime.setText(formattedDate);
}
}
}
答案 0 :(得分:0)
我不知道你是否发现了这个问题,但正如Frank van Puffelen所说,你必须使用equals
方法来比较java字符串。
所以,我建议你使用那个
if (m.getFrom().equals(currentUser)) {
msg.setBackgroundResource(R.drawable.chat_bubble2);
params = (RelativeLayout.LayoutParams)msg.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_END);
}
要比较日期,最好使用java方法:查看代码,了解两个日期是否相等,使用isEqual method。我建议你改变
if (now.get(Calendar.DATE).isEqual(msgTime.get(Calendar.DATE))) {
// your code
}
而不是减去知道日期是否明天,而是使用isAfter方法
else if (msgTime.get(Calendar.DATE).isAfter(now.get(Calendar.DATE))){
// your code
}
也请参阅此答案:How to compare dates in Java?我更喜欢 gstackoverflow 用户的答案。
如果我错了,请纠正我。