Listview Changing Items During Scroll
Solution 1:
First of all define in getView()
method into convertView=null;
and comment else condition in this method and add int type = getItemViewType(position)
;.
Also remove static keyword from each View item like Textview, ImageView
in ViewHolder class.
NOTE: override getViewTypeCount() and getItemViewType() in your adapter.
if(convertView == null){
switch (type) {
case0:
//First view[Row layout]break;
case1:
//Second view[Row layout]break;
//another Case here....
convertView.setTag(holder);
//remove else part when used convertView =null;/*else {
holder = (MyViewHolder) row.getTag();
}*/
}
@Override
publicintgetItemViewType(int position) {
// TODO Auto-generated method stub
map = list.get(position);
message_type = map.get("message_type");
if (message_type.equalsIgnoreCase("TEXT")) {
return0;
} else {
return1;
}
}
@Override
publicintgetViewTypeCount() {
// TODO Auto-generated method stubif (getCount() != 0)
return getCount();
return2;
}
Do it now! Done!
Solution 2:
I found the solution thanks to @frozenkoi. Ended up being the static variables inside the ViewHolder causing problems. They are now simply public and the class is static and the issue has been solved.
privatestaticclassMyViewHolder {
public TextView adTitle;
public TextView region;
public TextView time;
public ImageView thumbnail;
}
Solution 3:
Just add these lines of code in your adapter class.
@Override
publicintgetViewTypeCount() {
if (getCount() != 0)
return getCount();
return1;
}
Hope it will solve your problem.
Solution 4:
Problem comes only when viewholder variables are defined static. Otherwise listview works good with or without viewholder in scroll.
Solution 5:
In my case revert back was forgotten in if else
condition:
/*public class ContactAdapter extends BaseAdapter {
public List<Contact> _data;
private ArrayList<Contact> arraylist;
Context _c;
ViewHolder holder;
public ContactAdapter(List<Contact> contacts, Context context) {
_data = contacts;
_c = context;
this.arraylist = new ArrayList<Contact>();
this.arraylist.addAll(_data);
}*/@Overridepublic View getView(finalint i, View convertView, final ViewGroup viewGroup) {
Viewview= convertView;
finalContactdata= (Contact) _data.get(i);
ViewHolder holder ;
if (view == null) {
Contextcontext= viewGroup.getContext();
LayoutInflaterinflater= LayoutInflater.from(context);
view = inflater.inflate(R.layout.contact_list, null,false);
holder = newViewHolder();
holder.title = (TextView) view.findViewById(R.id.name);
holder.imageView = (ImageView) view.findViewById(R.id.pic);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.title.setText(data.getName());
// Set image if is null stringif(!data.getThumb().equals("null")){
try {
BitmapmyBitmap= BitmapFactory.decodeFile(ImageStorage.getImage(data.getThumb()).getAbsolutePath());
holder.imageView.setImageBitmap(myBitmap);
}catch (Exception e){e.printStackTrace();
holder.imageView.setImageResource(R.mipmap.contactimage);
}
}else// I had forgotten this else
holder.imageView.setImageResource(R.mipmap.contactimage);
// same action with background colorif(data.getId()==data.getFromContact() ){
view.setBackgroundColor(Color.RED);
} else{
view.setBackgroundColor(Color.WHITE); //**revert back it**
}
return view;
}
ViewHolder
privatestaticclassViewHolder {
ImageView imageView;
TextView title;
}
Post a Comment for "Listview Changing Items During Scroll"