Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

like image 999
ridvanzoro Avatar asked Dec 02 '13 12:12

ridvanzoro


People also ask

How check ListView is empty or not in android?

just check if (cartlist. size()<0) then yours list is Empty.!


1 Answers

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.

like image 107
nsL Avatar answered Sep 16 '22 19:09

nsL