ViewHolder Listview - Custom Adapter [closed]
-
21-12-2019 - |
Вопрос
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;
}
Решение
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;
}
Другие советы
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!