0

I have been trying to implement a listview implementation where either textview is displayed or an imageview is displayed according to the data being entered to an ArrayList in activity using BaseAdapter.

My implementation for custom base adapter:

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import com.android.pls.R;
import com.android.pls.common.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author ANKIT
 *
 */
public class CommentSplashListViewAdapter extends BaseAdapter 
{
    Context context;
    LayoutInflater inflater;
    ImageLoader imageLoader;
    private List<CommentSplashList> comments = new ArrayList<CommentSplashList>();
    private ArrayList<CommentSplashList> arraylist;

    public CommentSplashListViewAdapter(Context context, List<CommentSplashList> comments)
    {
        this.context = context;
        this.comments = comments;
        this.arraylist = new ArrayList<CommentSplashList>();
        imageLoader = new ImageLoader(context);
    }

    public void setListItems(List<CommentSplashList> commentLists) 
    {
        this.comments = commentLists;
        this.arraylist.addAll(commentLists);
    }

    @Override
    public int getCount() 
    {
        return comments.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return comments.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public int getViewTypeCount() 
    { 
        return 2;
    }

    @Override
    public int getItemViewType(int position) 
    {
        CommentSplashList comment = comments.get(position);

        if(comment.getContainsUrl() == false)
            return 0;
        else
            return 1;

    }

    public static class WithoutSplashbackViewHolder 
    {
        TextView creatorUserName;
        ImageView creatorProfilePic;
        TextView userComment;
        TextView timeAgo;
        //ImageView splashbackImage;
    }

    public static class WithSplashbackViewHolder 
    {
        TextView creatorUserName;
        ImageView creatorProfilePic;
        //TextView userComment;
        TextView timeAgo;
        ImageView splashbackImage;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        CommentSplashList comment = comments.get(position);

        WithoutSplashbackViewHolder holder1 = null;
        WithSplashbackViewHolder holder2 = null;

        View v = convertView;

        if (getItemViewType(position) == 0) 
        {
            holder1 = new WithoutSplashbackViewHolder();

            if (v == null) 
            {
                //GET View 1
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.splashitem_comment_without_splashback, null);

                v = viewGroup;


                holder1.creatorProfilePic = (ImageView) v.findViewById(R.id.creatorProfilePic);
                holder1.creatorUserName = (TextView) v.findViewById(R.id.creatorUserName);
                holder1.timeAgo = (TextView) v.findViewById(R.id.commentTimeAgo);

                holder1.userComment = (TextView) v.findViewById(R.id.userComment);
                //holder.splashbackImage = (ImageView) v.findViewById(R.id.splashbackImage);


            } 

            // Set the results into TextViews
            holder1.creatorUserName.setText(comment.getCreatorUserName());
            holder1.timeAgo.setText(comment.getTimeAgo());
            // Set the results into ImageView
            if(comment.getCreatorProfilePic() != "null")
                imageLoader.DisplayImage(comment.getCreatorProfilePic(),
                        holder1.creatorProfilePic);
            holder1.userComment.setText(comment.getUserComment());

            return v;


        } 
        else 
        {
            holder2 = new WithSplashbackViewHolder();


            if (v == null) 
            {
                //GET View 2
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.splashitem_comment_with_splashback, null);

                v = viewGroup;


                holder2.creatorProfilePic = (ImageView) v.findViewById(R.id.creatorProfilePic);
                holder2.creatorUserName = (TextView) v.findViewById(R.id.creatorUserName);
                holder2.timeAgo = (TextView) v.findViewById(R.id.commentTimeAgo);

                //holder.userComment = (TextView) v.findViewById(R.id.userComment);
                holder2.splashbackImage = (ImageView) v.findViewById(R.id.splashbackImage);

            } 

            // Set the results into TextViews
            holder2.creatorUserName.setText(comment.getCreatorUserName());
            //holder2.creatorUserName.setText("username");
            holder2.timeAgo.setText(comment.getTimeAgo());
            // Set the results into ImageView
            if(comment.getCreatorProfilePic() != "null")
                imageLoader.DisplayImage(comment.getCreatorProfilePic(),
                        holder2.creatorProfilePic);
            imageLoader.DisplayImage(comment.getSplashbackImage(),
                    holder2.splashbackImage);

            return v;
        }
    }



}

I have also tried to remove the lines: if (v == null) and its loop, it removed the error but its not inflating the view withsplashbackviewholder.

I searched on stack and online but I found solutions for where they only had multiple layouts with textviews only and not imageviews.

asethi
  • 146
  • 12

1 Answers1

0

I guess you misunderstood the meaning of View Holders. You should add ViewHolder as a Tag for a convertView. General logic is the following:

if (converView == null) {
    convertView = inflateView();
    holder = new Holder(convertView);
    converView.setTag(holder);
} else {
    holder = (Holder)convertView.getTag();
}

also, if getItemViewTypeCount() = 2, then you have to have 2 item types, and not 3 (you have 0, 1, 2).

and show me please this method:

comment.getContainsUrl()
agamov
  • 4,407
  • 1
  • 27
  • 31
  • I tried that also just now, but the result is the same..Its not giving output for the - else(getItemViewType(position) == 1) block. – asethi Mar 19 '14 at 13:42
  • If you put a breakpoint on the line return 1; in getItemViewType, does it ever stop there? If not, there's your problem. – Sofi Software LLC Mar 19 '14 at 17:27