0

I have a list of messages from my two users and want to display these into two different colored and positioned chat bubbles. With my current code all my messages get populated into the listview without any problems, only the color and positioning is not working correctly.

In my Activity I have the following code which gets called on the 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);
}

Here is my XML layout:

<?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>

Here is my 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);
        }

    }
}
Riley MacDonald
  • 453
  • 1
  • 4
  • 15

1 Answers1

0

I don't know if you found the problem, but as Frank van Puffelen said, you have to use the equals method to compare java Strings.

So, I can suggest you use that

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);
    }

To compare dates, is better using java methods: Seeing your code, to know if both dates are equals, use isEqual method. I recommend you change

if (now.get(Calendar.DATE).isEqual(msgTime.get(Calendar.DATE))) {
    // your code
}

and, instead of substract to know if the date is tomorrow, use isAfter method

else if (msgTime.get(Calendar.DATE).isAfter(now.get(Calendar.DATE))){
    // your code
}

See this answer too: How to compare dates in Java? I prefer the answer from gstackoverflow user.

If I'm wrong, please, correct me.

Community
  • 1
  • 1
Jaco
  • 923
  • 2
  • 14
  • 28