Search code examples
androidlistviewandroid-adaptercustom-adapter

android:How to fill two list view of same activity with one adapter


I have two lists & one custom adapter..both lists have same structure

mDrawerList = (ListView) findViewById(R.id.lv_left_drawer);
mDrawerListBottom = (ListView) findViewById(R.id.lv_left_drawer_bottom);

adapter = new DrawerItemsCustomAdapter(this);
mDrawerList.setAdapter(adapter);    
    adapter = new DrawerItemsCustomAdapter(this);
    mDrawerListBottom.setAdapter(adapter);

Q: How can I identify which ListView is accessing adapter, how to identify in adapter class.

what is & how to use ?

   public Object getItem(int arg0) {
    return arg0;
}

public long getItemId(int arg0) {
    return arg0;
}

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

thanks.


Solution

  • It is bad practice to use the same reference of an adapter across two different ListViews because modifying the underlying data would require you to notify both listviews of a change.

    So instead of this

    adapter = new DrawerItemsCustomAdapter(this);
    mDrawerList.setAdapter(adapter);    
    adapter = new DrawerItemsCustomAdapter(this);
    mDrawerListBottom.setAdapter(adapter);
    

    Simply make two separate adapters.

    You can use the same adapter class because, as you said, both lists have the same structure.

    DrawerItemsCustomAdapter adapter1 = new DrawerItemsCustomAdapter(this);
    mDrawerList.setAdapter(adapter1);    
    DrawerItemsCustomAdapter adapter2 = new DrawerItemsCustomAdapter(this);
    mDrawerListBottom.setAdapter(adapter2);
    

    It is not clear where you have initialized the List of data for these adapters, though, since it is not a parameter to the adapter.