Search code examples
androidlistadapter

ListAdapter working incorrectly on scroll Android


I wanted to do a generic ListAdapter, where instead of inserting predefined view structures, you can add different views in each list item.

I have my ListAdapter:

public class ListAdapter extends BaseAdapter {    
    private HashMap<Integer, Integer> listDataTypes; //For each element of the list, defines if its an ítem or a separator
    private List<View> listData;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;

    public ListAdapter() {
        listDataTypes = new HashMap<Integer, Integer>();
        listData = new ArrayList<>();
    }

    public void addItem(final View item) {
        listDataTypes.put(listData.size(), TYPE_ITEM);
        listData.add(item);
        notifyDataSetChanged();
    }

    public void addSeparator(final View item) {
        listDataTypes.put(listData.size(), TYPE_SEPARATOR);
        listData.add(item);
        notifyDataSetChanged();
    }

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

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

    @Override
    public int getItemViewType(int position) {
        return listDataTypes.get(position);
    }

    @Override
    public View getItem(int position) {
        return listData.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            View itemView = listData.get(position);
            convertView = itemView;
        }

        return convertView;
    }   
}

Here is an example of a list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/tvInfo1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvInfo2"
        android:layout_toLeftOf="@+id/tvInfo3"
        android:layout_toStartOf="@+id/tvInfo3"
        android:layout_toEndOf="@id/tvInfo1"
        android:layout_toRightOf="@id/tvInfo1"
        android:layout_centerVertical="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:id="@+id/tvInfo3"
        android:layout_centerVertical="true"/>
</RelativeLayout>
</LinearLayout>

And how I use the list adapter:

protected void onCreate(Bundle savedInstanceState) {
    ...
    ListAdapter listAdapter = new ListAdapter();

    TextView tvHeader = new TextView(this);
    tvHeader.setText("I am a header");

    listAdapter.addSeparator(tvHeader);

    for(int i=0; i<50; i++){
        View itemView = layoutInflater.inflate(R.layout.layout_two_items_list_item, null);
        TextView tvInfo1= (TextView) itemView .findViewById(R.id.tvInfo1);
        TextView tvInfo2= (TextView) itemView .findViewById(R.id.tvInfo2);
        TextView tvInfo3= (TextView) itemView .findViewById(R.id.tvInfo3);
        tvInfo1.setText("Id " + i);
        tvInfo2.setText("Info " + i);
        tvInfo3.setText("Hour " + i);
        listAdapter.addItem(itemView);
    }

    ListView listView = (ListView)findViewById(R.id.listview);
    listView.setAdapter(listAdapter);
    listView.setSelector(android.R.color.transparent);
}

But for some reason, when I scroll the list, some items start to float, other are not shown, other are repeated, other changes places, etc. What am I doing wrong?

This is how is shown (incorrectly) enter image description here


Solution

  • It is a very bad practice what you are doing. Keeping an array of views in the memory. The whole purpose of a listview to minimise that. In order to avoid the repetition replace

    if (convertView == null) {
                View itemView = listData.get(position);
                convertView = itemView;
            }
    
    return convertView;
    

    with

    return listData.get(position);
    

    But if I were you I would seriously consider rewriting the way you are doing your listview.