Android Open Source - FirebaseGradleChat Chat List Adapter






From Project

Back to project page FirebaseGradleChat.

License

The source code is released under:

Apache License

If you think the Android project FirebaseGradleChat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.polak.firebase.adapter;
/*from  ww w.  j a va  2s . co m*/
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.firebase.client.Query;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import pl.polak.firebase.R;
import pl.polak.firebase.chat.model.Chat;

/**
* This class is an example of how to use FirebaseListAdapter. It uses the <code>Chat</code> class to encapsulate the
* data for each individual chat message
*/
public class ChatListAdapter extends FirebaseListAdapter<Chat> {

    private static final DateFormat CHAT_MSG_DATE_FORMAT = DateFormat.getDateInstance(DateFormat.FULL);

    // The username for this client. We use this to indicate which messages originated from this user
    private String username;

    public ChatListAdapter(Query ref, Activity activity, int layout, String username) {
        super(ref, Chat.class, layout, activity);
        this.username = username;
    }

    /**
* Bind an instance of the <code>Chat</code> class to our view. This method is called by <code>FirebaseListAdapter</code>
* when there is a data change, and we are given an instance of a View that corresponds to the layout that we passed
* to the constructor, as well as a single <code>Chat</code> instance that represents the current data to bind.
* @param view A view instance corresponding to the layout we passed to the constructor.
* @param chat An instance representing the current state of a chat message
*/
    @Override
    protected void populateView(View view, Chat chat) {
        // Map a Chat object to an entry in our listview
        String author = chat.getAuthor();
        TextView authorText = (TextView)view.findViewById(R.id.author);
        authorText.setText(author + ": ");
        // If the message was sent by this user, color it differently
        if (author.equals(username)) {
            authorText.setTextColor(Color.RED);
        } else {
            authorText.setTextColor(Color.BLUE);
        }
        ((TextView)view.findViewById(R.id.message)).setText(chat.getMessage());
        ((TextView)view.findViewById(R.id.date)).setText(CHAT_MSG_DATE_FORMAT.format(chat.getDate()));
    }
}




Java Source Code List

pl.polak.firebase.MainActivity.java
pl.polak.firebase.adapter.ChatListAdapter.java
pl.polak.firebase.adapter.FirebaseListAdapter.java
pl.polak.firebase.chat.model.Chat.java