Marcin Milejski
Marcin Milejski

Reputation: 479

ListView disappearing after getting data

I have a Fragment with a ListView. After setting ListView's emptyView everything works ok, but as soon as my adapter (which is extending BaseAdapter) gets some data not only emptyView but also whole Fragment containing my ListView and few other Views disappear. If I do not use empty view everything works as it should.

Does my adapter need to implement something special for empty view to work? Or it is the way I set emptyView?

LayoutInflater inflater = getActivity().getLayoutInflater();

View emptyView = inflater.inflate(R.layout.empty_list_view,
(ViewGroup) mListView.getParent());

mListView.setEmptyView(emptyView);

Thank you,

Marcin.

Upvotes: 1

Views: 1011

Answers (1)

Marcin Milejski
Marcin Milejski

Reputation: 479

So I figured it out.

I now add empty view directly the place it should be in XML and then setEmptyView of my ListView in Java, and that works like I wanted it to work.

XML:

<ListView
   android:id="@+id/list_view"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />    
<TextView
   android:id="@+id/empty_view"          
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:text="@string/empty_list_view" />

and Java part, setting emptyView:

View emptyView = getActivity().findViewById(R.id.empty_view);
mListView.setEmptyView(emptyView);

Adapter should implement isEmpty():

@Override
public boolean isEmpty()
{
   return listItems.size() == 0;
}

I hope it will help someone in the future. ;)

Upvotes: 3

Related Questions