Skip to content

Instantly share code, notes, and snippets.

@muzafakar
Created December 5, 2019 23:02
Show Gist options
  • Save muzafakar/78eabeb3886904f301285e8d3737ca52 to your computer and use it in GitHub Desktop.
Save muzafakar/78eabeb3886904f301285e8d3737ca52 to your computer and use it in GitHub Desktop.
public class Chat {
private String username;
private String message;
public Chat() {
// Default Constructor kosong
}
public Chat(String username, String message) {
this.username = username;
this.message = message;
}
public String getUsername() {
return username;
}
public String getMessage() {
return message;
}
}
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private List<Chat> chatList;
private Context context;
public ChatAdapter(Context context) {
this.context = context;
chatList = new ArrayList<>();
}
public void setData(List<Chat> data) {
chatList.clear();
chatList.addAll(data);
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// R.layout.namaLayout
View v = LayoutInflater.from(context).inflate(R.layout.item_chat, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bindView(chatList.get(position));
}
@Override
public int getItemCount() {
return chatList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tvUsername, tvMessage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tvUsername = itemView.findViewById(R.id.tvUsername);
tvMessage = itemView.findViewById(R.id.tvMessage);
}
public void bindView(Chat chat) {
tvMessage.setText(chat.getMessage());
tvUsername.setText(chat.getUsername());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment