ridvanzoro
ridvanzoro

Reputation: 646

Android ListView,show a message if arrayList of arrayAdapter is empty

I need help. I have a Listview.setListAdapter(arrayAdapter) and array adapter have a arrayList.

If my arrayList is empty it shows loading images, it is normal, but how can i show a message if my arrayList is empty? Thanks in advance. enter image description here

Upvotes: 6

Views: 6821

Answers (2)

nsL
nsL

Reputation: 3792

The common way is setting an empty textview (@android:id/empty) after the listview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:dividerHeight="1dp" >
</ListView>

<TextView 
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/empty_list" />

</LinearLayout>

And then set set the empty view to the listview:

ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);

This should show items in the list whenever they are, and show the textview when not.

Upvotes: 13

grexter89
grexter89

Reputation: 1102

With a Toast.

if(list == null || list.size() == 0)
    Toast.makeText(context, "The list is empty", Toast.LENGTH_SHORT).show();

Upvotes: 0

Related Questions