Luke
Luke

Reputation: 3541

Displaying an alternative to an empty list in android

I want to be able to show an alternative to an empty list, currently the following method call sets up the list, however taskArrayList could be empty if the call to the database has returned an empty list

/**
 * Setup list adapter.
 */
private void setupListAdapter() {
setListAdapter(new TaskAdapter(this, R.id.tasks_list_view,
    taskArrayList));

taskListView = getListView();
taskListView.setOnItemClickListener(new TaskClickListener());
}

I'm thinking that before I call this method, or at the beginning of the method adding the following check

if(taskArrayList.isEmpty()) {
    /*load an alternative view*/
}

however because my Activity is a ListActivity so it requires a ListView to be set against it, so I'm unsure of the best approach to handling the case when the list is empty.

I don't want to create a "dummy item" which says there are no items and add it to the ArrayList but I'm not sure of the alternatives.

Upvotes: 0

Views: 571

Answers (1)

Jeroen Coupé
Jeroen Coupé

Reputation: 1404

If you add something with the idea @android:id/empty

it will be shown instead of the list when it is empty automagically!

 <TextView android:id="@android:id/empty"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#FF0000"
           android:text="No data"/>

In the documentation they say it like this:

Optionally, your custom view can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:id/empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.

http://developer.android.com/reference/android/app/ListActivity.html

Upvotes: 3

Related Questions