-1

I've been reading up on Viewholders for listview and the benefits are such that they can help improve the performance of the listview scrolling.I've been trying to implement it for my CustomAdapter but it seems that I'm doing it wrong somewhere(keeps crashing).So how do I go about to implement a viewholder in my Custom Adapter?

                    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        convertView = myInflater.inflate(R.layout.activity_list_view,
                parent, false);
    } else {
        convertView = myInflater.inflate(R.layout.activity_list_view,
                parent, false);
    }
    TextView name = (TextView) convertView.findViewById(R.id.feed_post);
    final String isLiked;
    TextView date = (TextView) convertView.findViewById(R.id.duration);
    HashMap<String, String> Item = new HashMap<String, String>();
    ImageView image = (ImageView) convertView.findViewById(R.id.comments);
    ImageView image_thumbs = (ImageView) convertView
            .findViewById(R.id.thumbs);
    Item = arrayList.get(position);
    // Get the time
    time = Item.get("Time");
    // Break up the time
    new_time = time.split("T")[0];
    isLiked = Item.get("Liked");
    // Split more
    get_splitted_time = new_time.split("-");
    // Get the month
    Year = get_splitted_time[0];
    Day = get_splitted_time[2];
    if (isLiked == null || isLiked == "false") {
        image_thumbs.setImageDrawable(convertView.getResources()
                .getDrawable(R.drawable.thumbsup_liked_new_flattened));
    }
    if (isLiked == "true") {
        image_thumbs
                .setImageDrawable(convertView.getResources().getDrawable(
                        R.drawable.thumbsup_liked_state_new_flattened));
    }
    if (get_splitted_time[1].startsWith("0")) {
        Month = months[Integer.valueOf(get_splitted_time[1].substring(1)) - 1];
    } else {
        Month = months[Integer.valueOf(get_splitted_time[1]) - 1];
    }
    image_thumbs.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isLiked == "true") {
                ;
                HashMap<String, String> Item = new HashMap<String, String>();
                Item = arrayList.get(position);
                Item.put("Liked", "false");
                unlikePost(feed_item_post_id.get(position));
            }
            CustomAdapter.this.notifyDataSetChanged();
        }
    });
    // Onclick listener for comments
    image.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(context, SinglePost.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            myIntent.putExtra("post_id", feed_item_post_id.get(position));
            myIntent.putExtra("liked", true);
            myIntent.putExtra("position", 0);
            context.startActivity(myIntent);

        }
    });
    final_time = Day + " " + Month + " " + Year;
    name.setText(Item.get("Message"));
    date.setText(final_time);
    return convertView;
}
Arvind Dinivra
  • 357
  • 1
  • 3
  • 14

2 Answers2

0

There is a very good tutorial about lists from a guy called lars vogel. Part of it is the holder pattern. Here is the direct link:

http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder

On this website you'll find awesome android-tutorials about pretty much anything. i highly recommend the site. If you still have questions about the holder after reading the paragraph in the tutorial...just write a comment here.

cheers!

PLM57
  • 1,256
  • 12
  • 27
  • 1
    the tutorial could also be good, but how does it answer to the question? – Blackbelt Jun 04 '14 at 08:19
  • You are right...i could have focused more on the code. After flying over it i thought the problem seems to be less a coding- but more of an understanding-of-concept-problem :)! The obvious faults were commented already (isliked and the initial inflating part). But without a proper dump of the logcat-output its difficult. My answer should be a starting point for @arvind to compare his code. – PLM57 Jun 04 '14 at 08:21
  • @blackbelt: i jus reread the initial request of his question and it really isn't all the clear. First part of the paragraph is asking for a conceptual description while the second half is asking for a code-correction.... 50:50 :D! Anyway...i gave your concern here an upvote! – PLM57 Jun 04 '14 at 08:26
  • don't take it personally. It is not about downvote/upvote. *Imo*, you should have posted a comment on the post with the link to the tutorial. – Blackbelt Jun 04 '14 at 08:28
  • like i wrote: You are right! I'm not taking it personally. That's why i gave your remark an upvote...because it is right! – PLM57 Jun 04 '14 at 08:36
  • @PLM57 & blackbelt,thank you guys for the helping me out,I've already solved the issue,it seems that when I was using the viewHolder,I missed out on setting that Tag for it.That caused it to crash.Thank you once again for helping me out :) – Arvind Dinivra Jun 04 '14 at 08:38
0

Here is an example on how to implement ViewHolder.
Just giving you a little hint.

    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = myInflater.inflate(R.layout.activity_list_view,
                    parent, false);
              ViewHolder holder = new ViewHolder();
              holder.name = (TextView) convertView.findViewById(R.id.feed_post);
              holder.date = (TextView) convertView.findViewById(R.id.duration);
              holder.image = (ImageView) convertView.findViewById(R.id.comments);
              convertView.setTag(holder); // dont forget this one

        } else {
            holder = (ViewHolder) convertView.getTag(); // recycle
        }

    ....
    // how to use it
        holder.name.setText(Item.get("Message"));
        holder.date.setText(final_time);
        return convertView;


    }

// define your holder class
        private static class ViewHolder{
            TextView name;
            TextView date;
            ImageView image;
        }
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • It seems that I didn't set the tag for the holder,and I've solved my issue through your help.A huge thank you @LazyNinja :) – Arvind Dinivra Jun 04 '14 at 08:40