ListView Changing Items During Scroll

I am implementing a ListFragment using a custom ArrayAdapter to populate the list. Each row item has an ImageView and three TextViews. Data is being parsed via XML and the images are being async loaded.

The problem I am having is that the ListView populates and looks good, but there is a problem when scrolling. I can fit 7 items on the screen at once. When I scroll to the 8th, it changes suddenly so the next row that should be appearing. It only does it on rows divisible by 8 (ie. rows 8, 16, 24, etc).

I'm using the ViewHolder pattern to ensure good speed with this ListView. I figure the problem lies in there somewhere, but I have searched around and it appears I am doing this pattern correctly and I have run out of things to check in order to resolve this issue. What am I doing wrong? Thanks!

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    MyViewHolder holder;
    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        row = inflater.inflate(R.layout.browse_result_list_item, null, false);
        holder = new MyViewHolder();
        holder.adTitle = (TextView) row.findViewById(R.id.adTitle);
        holder.region = (TextView) row.findViewById(R.id.region);
        holder.time = (TextView) row.findViewById(R.id.time);
        holder.thumbnail = (ImageView) row.findViewById(R.id.browseThumbnail);
        row.setTag(holder);
    } else {
        holder = (MyViewHolder) row.getTag();
    }
    SearchResult result = mObjects.get(position);

    holder.adTitle.setText(result.getTitle().substring(0, result.getTitle().length()-3)); // three ... at the end, remove them
    holder.region.setText(result.getRegion());
    holder.time.setText(result.getPostingTime());

    // Download the image thumbnail
    ArrayList<String> urls = result.getImageUrls();
    if (urls.size() > 0)
        download(urls.get(0), holder.thumbnail);
    else // No image for this post, put a placeholder
        holder.thumbnail.setImageResource(R.drawable.ic_action_picture);

    return row;
}

private static class MyViewHolder {
    public static TextView adTitle;
    public static TextView region;
    public static TextView time;
    public static ImageView thumbnail;
}

Edit: 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 bee solved.


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) {
            case 0:
                  //First view[Row layout]
            break;
            case 1:
                 //Second view[Row layout]
            break;

            //another Case here....

   convertView.setTag(holder);

  //remove else part when used convertView =null;
        /*else {
        holder = (MyViewHolder) row.getTag();
    }*/

} 

@Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        map = list.get(position); 
        message_type = map.get("message_type");
        if (message_type.equalsIgnoreCase("TEXT")) {
            return 0;
        } else {
            return 1;
        }

    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        if (getCount() != 0)
            return getCount();
        return 2;
    }

Do it now! Done!

android - ListView Changing Items During Scroll, First of all define in getView() method into convertView=null; and comment else condition in this method and add int type  @Marek said in ListView does not change currentIndex when scrolling: The problem is that when I "swipe" pictures ListView.currentIndex is not updated. and thats good ;) The current index shouldn't change during scrolling. If you need that you need to listen to scroll updates and set the current index yourself accordingly.


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.

private static class MyViewHolder {
    public TextView adTitle;
    public TextView region;
    public TextView time;
    public ImageView thumbnail;
}

Keeping the data in ListView with interactive items while scrolling in , What happened here is when ever we scroll a list view the items redraws itself again from the scrach and the changes we made in edit text and check box had  Recyclerview Changing Items During Scroll. Questions: I have a RecyclerView. Each row has a play button, textview and Progressbar. when click on the play button have to play audio from my sdcard and have to progress Progressbar.


Just add these lines of code in your adapter class.

@Override
public int getViewTypeCount() {

    if (getCount() != 0)
        return getCount();

    return 1;
}

Hope it will solve your problem.

Android listview items shuffling when scrolling from top to bottom , I would suggest you to use recycler view instead of ListView. Recycler view comes with more decoration and methods which will definitely help  ChoosingItemContainer is an event that allows you to provide an item (ListViewItem / GridViewItem) to the ListView / GridView whenever a new item is needed during start-up or recycling. You can create a container based on the type of data item the container will display (shown in the example below).


Problem comes only when viewholder variables are defined static. Otherwise listview works good with or without viewholder in scroll.

ListView content background changed when I scroll down and up , If the condition is true I am changing the background of the content.If not keeping as it is. The problem is when I go to this page which is displaying  ListView Changing Items During Scroll. I am implementing a ListFragment using a custom ArrayAdapter to populate the list. Each row item has an ImageView and three TextViews. Data is being parsed via XML and the images are being async loaded. The problem I am having is that the ListView populates and looks good, but there is a problem when scrolling.


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);
}*/
@Override
public View getView(final int i,  View convertView, final ViewGroup viewGroup) {
    View view = convertView;
    final Contact data = (Contact) _data.get(i);
    ViewHolder holder ;

    if (view == null) {
        Context context = viewGroup.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.contact_list, null,false);
        holder = new ViewHolder();
        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 string
    if(!data.getThumb().equals("null")){
        try {
             Bitmap myBitmap = 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 color
    if(data.getId()==data.getFromContact() ){
         view.setBackgroundColor(Color.RED);
     } else{
         view.setBackgroundColor(Color.WHITE); //**revert back it**
     }
        return view;
}

ViewHolder

  private  static class ViewHolder {
    ImageView imageView;
    TextView title;

}

Recyclerview Changing Items During Scroll, RecyclerView is an advancement of listView. Android RecyclerView - scrolling changes items. It's a fact that list views (or more generic - collection views) are the​  I have a listview and a button which when clicked loads the listview with some items from an xml file. When the user clicks it again (because they might want to update the data in the listview to see if it has changed) it throws a NullReferenceException.


ListView, In order to display items in the list, call setAdapter(android.widget. See Making ListView Scrolling Smooth for more ways to ensure a smooth user experience. The list will automatically scroll to the bottom when a data set change This is called during layout when the size of this view has changed. The problem is that you are creating a new adapter every time you reload the data. That's not how you should use ListView and its adapter. Instead of setting a new adapter (which causes ListView to reset its state), simply update the content of the adapter already set on the ListView. And the selection/scroll position will be saved for you.


RecyclerView shuffles items and changing values on scrolling fast , Whenever you are working with a RecyclerView. You may face an issue that item shuffles a lot and it's changing the position of list items during fast scrolling. First is a selection in the listview. Second, the item selected though the world. For example, a linetrace from the character. If the Item and character are informed of the selection after the trace, and then the Listview is told to select the item it creates unnecessary logic, which might be problematic.


Android recyclerview changing items during scroll, Android recyclerview changing items during scroll. If you are using ListView - override the following methods. com. If you need help setting up the  A ListView control allows you to display a list of items with item text and, optionally, an icon to identify the type of item. For example, the Windows Explorer list of files is similar in appearance to a ListView control. It displays a list of the files and folders currently selected in the tree. Each file and folder displays an icon