Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firebase Workshop RealtimeDatabase 관련 코드 조각들
package com.gdgkr.firebaseworkshop;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
public class MessageDataAdapter
extends FirebaseRecyclerAdapter<MessageData, MessageDataAdapter.MessageViewHolder> {
public MessageDataAdapter(DatabaseReference ref) {
super(MessageData.class, 0, MessageViewHolder.class, ref);
}
/*
MessageViewHolder
*/
public static class MessageViewHolder extends RecyclerView.ViewHolder {
final Context ViewContext;
final TextView TextView;
final ImageView ImageView;
public MessageViewHolder(View itemView) {
super(itemView);
TextView = (TextView) itemView.findViewById(R.id.message_item_text);
ImageView = (ImageView) itemView.findViewById(R.id.message_item_user_photo);
ViewContext = itemView.getContext();
}
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MessageData.MessageType msgType = MessageData.MessageType.valueOf(viewType);
final int layout;
switch (msgType) {
case MESSAGE_FROM_ME:
layout = R.layout.message_item_mine;
break;
case MESSAGE_FROM_OTHERS:
layout = R.layout.message_item_others;
break;
default:
layout = R.layout.message_item_mine;
break;
}
View itemView = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
return new MessageViewHolder(itemView);
}
@Override
public int getItemViewType(int position) {
return getItem(position).getMessageType().ordinal();
}
@Override
protected void populateViewHolder(MessageViewHolder holder, MessageData msg, int position) {
holder.TextView.setText(msg.getText());
if (msg.getPhotoUrl()== null) {
holder.ImageView.setImageDrawable(ContextCompat.getDrawable(holder.ViewContext,
R.drawable.firebase_logo));
} else {
Glide.with(holder.ViewContext)
.load(msg.getPhotoUrl())
.into(holder.ImageView);
}
}
@Override
protected MessageData parseSnapshot(DataSnapshot snapshot) {
return snapshot.getValue(MessageData.class);
}
}
//in MessageListFragment.java
private void sendMessage(String message) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final String photoUrl = user.getPhotoUrl() != null ? user.getPhotoUrl().toString() : null;
MessageData msg = new MessageData(user.getDisplayName(), message, photoUrl);
FirebaseDatabase.getInstance().getReference().child("chat").push().setValue(msg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment