I have an Activity, which simply consists in listing Pair<String, String> objects. I have a custom TextWithSubTextAdapter, which extends ArrayAdapter:
public View getView(int position, View convertView, ViewGroup parent)
{View view; if (convertView == null) { LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = li.inflate(R.layout.text_sub, null); TextView tv = (TextView)view.findViewById(R.id.mainText); tv.setText(mCategories.get(position).first); TextView desc = (TextView)view.findViewById(R.id.subText); desc.setText(Html.fromHtml(mCategories.get(position).second)); } else { view = (View) convertView; } return view;}
mCategories is an ArrayList<Pair<String, String>>
I then call lv.setAdapter(new TextSubTextAdapter(this, Common.physConstants));
As long as I have a limited set of elements, It works great, because I don’t need to scroll. However, when I add enough elements, after scrolling, the items swap their positions, like this:


I suspect that this behavior is due to me calling mCategories.get(position). Because the Views are never kept in the background and Android regenerates them every time, I never get the same item, as position will rarely have the same value.
Is there a way to get a constant id, which could allow me to get items with fixed positions ? I tried to use getItemID, but I do not understand how to implement it.
Note: Every string comes from a strings.xml file. They are never compared, and instanciated once, at startup.
When you scroll your list Android dynamically reuses the Views which scroll out of the screen. These convertViews don’t have the content which should be at this position yet. You have to set that manually.