我一直在阅读 Viewholders for listview 的好处是它们可以帮助提高 listview 滚动的性能。我一直在尝试为我的 CustomAdapter 实现它,但似乎我在某处做错了(不断崩溃)。那么我该如何在我的自定义适配器中实现一个视图?
@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;
}