package com.sinch.messagingtutorial.app;

import android.app.Activity;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sinch.android.rtc.messaging.Message;

import java.util.ArrayList;
import java.util.List;

public class MessageAdapter extends BaseAdapter {

    public static final int DIRECTION_INCOMING = 0;
    public static final int DIRECTION_OUTGOING = 1;

    private List<Pair<Message, Integer>> messages;
    private LayoutInflater layoutInflater;

    public MessageAdapter(Activity activity) {
        layoutInflater = activity.getLayoutInflater();
        messages = new ArrayList<Pair<Message, Integer>>();
    }

    public void addMessage(Message message, int direction) {
        messages.add(new Pair(message, direction));
        notifyDataSetChanged();
    }

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

    @Override
    public Object getItem(int i) {
        return messages.get(i);
    }

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

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        int direction = messages.get(i).second;

        if (convertView == null) {
            int res = 0;
            if (direction == DIRECTION_INCOMING) {
                res = R.layout.message_left;
            } else if (direction == DIRECTION_OUTGOING) {
                res = R.layout.message_right;
            }
            convertView = layoutInflater.inflate(res, viewGroup, false);
        }

        Message message = messages.get(i).first;

        TextView txtMessage = (TextView) convertView.findViewById(R.id.txtMessage);
        txtMessage.setText(message.getTextBody());

        return convertView;
    }
}


@@ -64,16 +64,41 @@
 tivity;%0A
+import android.util.Log;%0A
 import a
@@ -1184,32 +1184,207 @@
 eturn i;%0A    %7D%0A%0A
+    @Override%0A    public int getViewTypeCount() %7B%0A        return 2;%0A    %7D%0A%0A    @Override%0A    public int getItemViewType(int i) %7B%0A        return messages.get(i).second;%0A    %7D%0A%0A
     @Override%0A  
@@ -1477,38 +1477,34 @@
 ction = 
-messages.get(i).second
+getItemViewType(i)
 ;%0A%0A     
@@ -1587,34 +1587,17 @@
 tion == 
-DIRECTION_INCOMING
+0
 ) %7B%0A    
@@ -1635,11 +1635,12 @@
 age_
-lef
+righ
 t;%0A 
@@ -1674,34 +1674,17 @@
 tion == 
-DIRECTION_OUTGOING
+1
 ) %7B%0A    
@@ -1710,36 +1710,35 @@
 .layout.message_
-righ
+lef
 t;%0A            %7D
@@ -1816,24 +1816,106 @@
 );%0A        %7D
+ else if (convertView != null) %7B%0A            Log.d(%22sinch%22, %22not null%22);%0A        %7D
 %0A%0A        Me

Fix messaging adapter to show messages on the correct side