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;
}