Zakynthos February 2016

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.

Answers


cricket_007 February 2016

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.


Crew Peace February 2016

Your question isn't very clear. I think you are confused, but correct me if I'm wrong.

You are reusing the same object for creating the adapter for the other ListView, thus overwriting the first. I assume you want to handle them separately so you should use a new object:

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

Otherwise, there is no way to determine which instance of a ListView is using the Adapter at any time.

You should be using different Adapters for different ListViews. An Adapter should know how to populate a ListView in a single way.


Sohail Zahid February 2016

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

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

           mDrawerList.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String s = adapter.getItem(position);
                    }
                });
           mDrawerListBottom.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                  String s = adapter1.getItem(position);
                    }
                });

Post Status

Asked in February 2016
Viewed 1,511 times
Voted 7
Answered 3 times

Search




Leave an answer