0

This is the adapter. The behaviour is as described in the title, if I scroll to the bottom and then back up the order of the elements changes. I can't figure out why the order of the items would be changing.

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

class CustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;

    List<Map<String, String>> mData = new ArrayList<Map<String, String>>();
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();

    private LayoutInflater mInflater;

    public CustomAdapter(Context context) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final Map<String, String> item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSectionHeaderItem(final Map<String, String> item) {
        mData.add(item);
        sectionHeader.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position){
        return mData.get(position).get("id");
    }

    public String getItemMain(int position) {
       return mData.get(position).get("id");
   }

    public String getItemSecondary(int position) {
        return mData.get(position).get("description");
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int rowType = getItemViewType(position);

        if (convertView == null) {
            holder = new ViewHolder();
            switch (rowType) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.snippet_item1, null);
                    holder.textView = (TextView) convertView.findViewById(R.id.text);
                    holder.textView2 = (TextView) convertView.findViewById(R.id.text2);
                    holder.textView.setText(mData.get(position).get("id"));
                    holder.textView2.setText(mData.get(position).get("description"));
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.snippet_item2, null);
                    holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
                    holder.textView.setText(mData.get(position).get("id"));
                    //holder.textView.setText(mData.get(position).get("description")); //no sub item yet!
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //holder.textView.setText(mData.get(position));


        return convertView;
    }

    public static class ViewHolder {
        public TextView textView;
        public TextView textView2;
    }

}
AlexF11
  • 231
  • 1
  • 5
  • 20

1 Answers1

0

You're using the ViewHolder patter in a incomplete way. If currentView != null, you still need to update the cell information (textviews), otherwise you're using the same recycled view, with the old information loaded.

So:

if (currentView == null) {
    // this is ok
} else {
    holder = (ViewHolder) convertView.getTag();
    // this is the new stuff
    holder.textView.setText("blablabla");
}
Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58