Reputation: 109
I am getting wrong data after scrolling listview,after scrolling images are show on another listitem.
public class ContactListAdapter extends BaseAdapter {
private List<ContactBean> contactDataList;
private ArrayList<ContactBean> arraylist;
Context context;
ViewHolder v;
public ContactListAdapter(List<ContactBean> contactBeans, Context context) {
contactDataList = contactBeans;
this.context = context;
this.arraylist = new ArrayList<ContactBean>();
this.arraylist.addAll(contactDataList);
}
@Override
public int getCount() {
return contactDataList.size();
}
@Override
public Object getItem(int i) {
return contactDataList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (view == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.contact_list_item, null);
Log.e("Inside", "here--------------------------- In view1");
} else {
view = convertView;
Log.e("Inside", "here--------------------------- In view2");
}
v = new ViewHolder();
v.title = (TextView) view.findViewById(R.id.name);
v.phone = (TextView) view.findViewById(R.id.no);
v.imageView = (RoundedImageView) view.findViewById(R.id.pic);
v.firstChar = (TextView) view.findViewById(R.id.tv_firstChae);
final ContactBean data = contactDataList.get(i);
v.title.setText(contactDataList.get(i).getName());
v.phone.setText(contactDataList.get(i).getPhone());
// Set image if exists
try {
if (contactDataList.get(i).getThumb() != null) {
v.firstChar.setVisibility(View.GONE);
v.imageView.setVisibility(View.VISIBLE);
v.imageView.setImageBitmap(data.getThumb());
} else {
v.imageView.setVisibility(View.GONE);
String headerChar = (String) contactDataList.get(i).getName().subSequence(0, 1);
v.firstChar.setText(headerChar);
}
} catch (OutOfMemoryError e) {
// Add default picture
// v.imageView.setImageDrawable(this.context.getDrawable(R.mipmap.ic_launcher));
e.printStackTrace();
}
Log.e("Image Thumb", "--------------" + contactDataList.get(i).getThumb());
view.setTag(data);
return view;
}
static class ViewHolder {
RoundedImageView imageView;
TextView title, phone, firstChar;
}
}
in my contact list some contacts having image and which one not having a image thst show the first letter of initialletter of contact name
enter image description here run the app i am geting this but scroool the listview the we getenter image description here
Upvotes: 1
Views: 735
Reputation: 555
Try this:
UPD:
public class ContactListAdapter extends BaseAdapter {
private List<ContactBean> contactBeans;
private Context context;
public ContactListAdapter(List<ContactBean> contactBeans, Context context) {
this.context = context;
this.contactBeans = contactBeans;
}
@Override
public int getCount() {
return contactBeans.size();
}
@Override
public Object getItem(int i) {
return contactBeans.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
ViewHolder v;
View view = convertView;
if (view == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.contact_list_item, viewGroup, false);
v = new ViewHolder();
v.title = (TextView) view.findViewById(R.id.name);
v.phone = (TextView) view.findViewById(R.id.no);
v.imageView = (RoundedImageView) view.findViewById(R.id.pic);
v.firstChar = (TextView) view.findViewById(R.id.tv_firstChae);
view.setTag(v);
}
v = (ViewHolder)view.getTag();
final ContactBean data = contactBeans.get(i);
v.title.setText(data.getName());
v.phone.setText(data.getPhone());
// Set image if exists
try {
if (data.getThumb() != null) {
v.firstChar.setVisibility(View.GONE);
v.imageView.setVisibility(View.VISIBLE);
v.imageView.setImageBitmap(data.getThumb());
} else {
v.imageView.setVisibility(View.GONE);
String headerChar = (String) data.getName().subSequence(0, 1);
v.firstChar.setText(headerChar);
}
} catch (OutOfMemoryError e) {
// Add default picture
// v.imageView.setImageDrawable(this.context.getDrawable(R.mipmap.ic_launcher));
e.printStackTrace();
}
Log.e("Image Thumb", "--------------" + data.getThumb());
return view;
}
static class ViewHolder {
RoundedImageView imageView;
TextView title, phone, firstChar;
}
}
Upvotes: 1
Reputation: 301
Your simply missing to set the character visible, due to it possibly being invisible from being recycled.
The correct code (containing Ricardo Barroca's improvment):
public class ContactListAdapter extends BaseAdapter {
private List<ContactBean> contactDataList;
private ArrayList<ContactBean> arraylist;
Context context;
ViewHolder v;
public ContactListAdapter(List<ContactBean> contactBeans, Context context) {
contactDataList = contactBeans;
this.context = context;
this.arraylist = new ArrayList<ContactBean>();
this.arraylist.addAll(contactDataList);
}
@Override
public int getCount() {
return contactDataList.size();
}
@Override
public Object getItem(int i) {
return contactDataList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
ViewHolder v;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.contact_list_item, null);
v = new ViewHolder();
convertView.setTag(v);
} else {
v = convertView.getTag();
}
View view = convertView;
v.title = (TextView) view.findViewById(R.id.name);
v.phone = (TextView) view.findViewById(R.id.no);
v.imageView = (RoundedImageView) view.findViewById(R.id.pic);
v.firstChar = (TextView) view.findViewById(R.id.tv_firstChae);
final ContactBean data = contactDataList.get(i);
v.title.setText(contactDataList.get(i).getName());
v.phone.setText(contactDataList.get(i).getPhone());
// Set image if exists
try {
if (contactDataList.get(i).getThumb() != null) {
v.firstChar.setVisibility(View.GONE);
v.imageView.setVisibility(View.VISIBLE);
v.imageView.setImageBitmap(data.getThumb());
} else {
v.imageView.setVisibility(View.GONE);
String headerChar = (String) contactDataList.get(i).getName().subSequence(0, 1);
v.firstChar.setVisibility(View.VISIBLE);
v.firstChar.setText(headerChar);
}
} catch (OutOfMemoryError e) {
// Add default picture
// v.imageView.setImageDrawable(this.context.getDrawable(R.mipmap.ic_launcher));
e.printStackTrace();
}
Log.e("Image Thumb", "--------------" + contactDataList.get(i).getThumb());
view.setTag(data);
return view;
}
static class ViewHolder {
RoundedImageView imageView;
TextView title, phone, firstChar;
}
}
Upvotes: 0
Reputation: 1887
Remove the global ViewHolder
ViewHolder v;
The scope of ViewHolder instance have to be inside the getView method only not whole adapter.
Replace the
v = new ViewHolder();
with
ViewHolder v = new ViewHolder();
here i cannot find any use of viewHolder actually, you can simply try like this
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (view == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.contact_list_item, null);
Log.e("Inside", "here--------------------------- In view1");
TextView title = (TextView) view.findViewById(R.id.name);
TextView phone = (TextView) view.findViewById(R.id.no);
RoundedImageView imageView = (RoundedImageView) view.findViewById(R.id.pic);
TextView firstChar = (TextView) view.findViewById(R.id.tv_firstChae);
final ContactBean data = contactDataList.get(i);
title.setText(contactDataList.get(i).getName());
phone.setText(contactDataList.get(i).getPhone());
// Set image if exists
try {
if (contactDataList.get(i).getThumb() != null) {
firstChar.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
imageView.setImageBitmap(data.getThumb());
} else {
imageView.setVisibility(View.GONE);
String headerChar = (String) contactDataList.get(i).getName().subSequence(0, 1);
firstChar.setText(headerChar);
}
} catch (OutOfMemoryError e) {
// Add default picture
// v.imageView.setImageDrawable(this.context.getDrawable(R.mipmap.ic_launcher));
e.printStackTrace();
}
Log.e("Image Thumb", "--------------" + contactDataList.get(i).getThumb());
} else {
view = convertView;
Log.e("Inside", "here--------------------------- In view2");
}
return view;
}
Upvotes: 0
Reputation: 9656
You should only initialize your viewHolder once, and thats when your convertView is null. Place this:
if (convertView == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.contact_list_item, null);
v = new ViewHolder();
convertView.setTag(v);
} else {
v = convertView.getTag();
}
Check this link for better understanding. It's from Google. Starts at about minute 6:00
https://www.youtube.com/watch?v=wDBM6wVEO70
Upvotes: 0