Android Open Source - Comment Adapter

From Project

Back to project page


The source code is released under:

Copyright (c) 2013 Karl Monaghan ( Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Soft...

If you think the Android project 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

//  w  w w. j a  va  2 s . c o m

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CommentAdapter extends ArrayAdapter<Comment> implements View.OnClickListener {
    public interface ReplyCommentListener {
        public void onReply(int commentId);

    ReplyCommentListener mListener;

    public static class ViewHolder {
        public ImageView commentAvatar;

        public TextView commentUser;

        public TextView commentDate;

        public TextView commentBody;

        public Button replyButton;

    public void setReplyCommentListener(ReplyCommentListener mListener) {
        this.mListener = mListener;

    public CommentAdapter(Context context, int textViewResourceId, List<Comment> comments) {
        super(context, textViewResourceId, comments);


    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        Activity activity = (Activity) getContext();

        ViewHolder holder;
        if (v == null) {

            LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.comment_list_item, parent, false);
            holder = new ViewHolder();
            holder.commentAvatar = (ImageView) v.findViewById(;
            holder.commentUser = (TextView) v.findViewById(;
            holder.commentDate = (TextView) v.findViewById(;
            holder.commentBody = (TextView) v.findViewById(;
            holder.replyButton = (Button) v.findViewById(;
        } else {
            holder = (ViewHolder) v.getTag();


        int standardMargin = activity.getResources().getDimensionPixelSize(R.dimen.standard_margin);

        final Comment comment = getItem(position);

        int newMargin = (standardMargin * 2 * comment.getChildLevel());
        v.setPadding(newMargin, v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom());

        if (comment != null) {
            String avatar = comment.getAvatar();

            if ((avatar != null) && (avatar.length() > 0)) {
                DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc()
                 * .displayer( new RoundedBitmapDisplayer(activity.getResources ().getDimensionPixelSize(
                 * R.dimen.standard_corner_radius)))

                ImageLoader.getInstance().displayImage(avatar, holder.commentAvatar, options);



        return v;

    public void onClick(View v) {
        int position = (Integer) v.getTag();

        final Comment comment = getItem(position);


Java Source Code List