6

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

4
  • 1
    If you can show an image then why cannot you show a message ? Commented Dec 2, 2013 at 12:59
  • 1
    check arraList.size() Commented Dec 2, 2013 at 12:59
  • just replace or add toast code with loading code Commented Dec 2, 2013 at 12:59
  • my listview is a fragment and this image is default, so i have to create another fragment if arraList.size()<1, but i dont want to create another fragment. My question is how to replace this image to another image or a text view without creating another fragment? Commented Dec 2, 2013 at 13:14

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

Comments

0

With a Toast.

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

1 Comment

A Toast is not ideal.you want the user to always see a message telling them there is nothing to display at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.