Dayerman
Dayerman

Reputation: 4003

How to detect if a listview has an adapter set

I want to know when my listview has no adapter set on it. I mean, is there a way to know when I have called setListAdapter(mAdapter); without a condition on my code?

I want to add a header to my listview, but just the first time as:

if(getListView().getAdapter() != null){
    WWDisplayUtils.addHeaderButton(mTaxis, mAirport, AirportDetailsTaxi.this);
    setListAdapter(mTaxiA);
}
else    
   mTaxiA.notifyDataSetChanged();

But is not doing what I want.

Upvotes: 3

Views: 5703

Answers (3)

FunkTheMonk
FunkTheMonk

Reputation: 10938

Shouldn't

if(getListView().getAdapter() != null)

be

if(getListView().getAdapter() == null)

If you've already set a header or footer view, getAdapter() will return a WrapperListAdapter instead of the one you set with setListAdapter()

Upvotes: 8

i_am_jorf
i_am_jorf

Reputation: 54600

Call getListAdapter()?

Upvotes: 0

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

Can't you just call getAdapter() and see if its null or at least see if it's not equal to what you thought it should be? So like this:

if(mListView.getAdapter() != mAdapter){
  //do one thing
}
else{
  //do another thing
}

Upvotes: 1

Related Questions