【问题标题】:FirebaseUI Adapter Not Populating ListView CorrectlyFirebaseUI 适配器未正确填充 ListView
【发布时间】:2016-09-04 18:55:11
【问题描述】:

我有一个来自我的两个用户的消息列表,并希望将它们显示在两个不同颜色和位置的聊天气泡中。使用我当前的代码,我的所有消息都会毫无问题地填充到列表视图中,只有颜色和位置无法正常工作。

在我的活动中,我有以下代码在 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);
        }

    }
}

【问题讨论】:

标签: android android-layout listview firebase firebaseui


【解决方案1】:

不知道你有没有发现问题,但正如Frank van Puffelen所说,你必须使用equals方法来比较java Strings。

所以,我可以建议你使用它

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 用户的答案。

如果我错了,请纠正我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 2015-11-22
    • 2018-02-10
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多