mardi 31 juillet 2018

Where / how to determine whether chat message was sent or received (android)

Android chat app. I'm using a temporary ArrayList on the client to make sure displaying the chat bubbles work as expected. Where and how do I determine if a message was sent or received?

ChatConversationFragment:

   messageArrayList.add(new ChatConversationMessage("Hello"));
    messageArrayList.add(new ChatConversationMessage("Yeah"));
    messageArrayList.add(new ChatConversationMessage("Yeah", R.drawable.redhead_1)); 
//this last one is received because it has two inputs

ChatConversationMesssage:

/**
 * The reason we have TWO of these methods with the same name is because sent
 * messages don't have an image. We need to have a separate method for that.
 **/
public ChatConversationMessage(String sentTextMessage) {
    mSentTextMessage = sentTextMessage;
}

public ChatConversationMessage(String receivedTextMessage, int imageResourceId) {
    mReceivedTextMessage = receivedTextMessage;
    mImageResourceId = imageResourceId;
}

ConversationMessageAdapter:

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View listItemView = convertView;
    Holder holder;
    ChatConversationMessage currentMessage = getItem(position);

    if(listItemView == null) {
        holder = new Holder();

        if(currentMessage){
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.chat_conversation_txt_sent, parent, false);
        }
    }


    TextView sentText = listItemView.findViewById(R.id.textview_chat_message_sent);
    sentText.setText(currentMessage.getSentTextMessage());

    TextView receivedText = listItemView.findViewById(R.id.textview_chat_message_received);
    receivedText.setText(currentMessage.getReceivedTextMessage());

    return listItemView;
}

So I was trying to put it in an if-statement in this last class (the adapter). But I don't know what method to make and where to place it so I can return a Boolean or something to make it display different chat bubbles based on if the message was sent or received.

Aucun commentaire:

Enregistrer un commentaire