Display No Product Message in ListView

I created some user interfaces in my Android apps, and there are some ListView controls - among other controls - inside the view. Because of this, I used "Activity" as the base activity class.

Now I need to display a simple message like "No item" when the ListView attached to my adapter is empty. I know this is possible using ListActivity, but I'm not sure which is the best approach for this?

+49
android
Apr 6 '11 at 11:17
source share
6 answers

You are looking for an empty ListActivity view:

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

If you are using a ListView, you can use the setEmptyView () method:

http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View)

+42
Apr 6 '11 at 11:20
source share

You may have an empty view without ListActivity ! The correct method is as follows

First add an โ€œempty viewโ€ to your XML layout under your list

 ... <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <TextView android:id="@+id/empty" android:text="Empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" /> ... 

Next, override the onContentChanged method of your activity and set the empty view of your list to empty:

 @Override public void onContentChanged() { super.onContentChanged(); View empty = findViewById(R.id.empty); ListView list = (ListView) findViewById(R.id.list); list.setEmptyView(empty); } 

What is it! Android will take care to hide / show the list and empty views when updating the adapter.

Magic

The decision about whether an empty view is displayed or not is handled by the ListView superclass, AdapterView . AdapterView registers a DataSetObserver on the adapter, so it is notified of every data change. This calls the checkFocus call in the AdapterView , which contains the following lines

 if (mEmptyView != null) { updateEmptyStatus((adapter == null) || adapter.isEmpty()); } 

and sets the visibility of the empty view depending on whether the adapter is empty or not.

+96
Apr 6 2018-11-11T00:
source share

Just connect the ListView to the TextView :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/list" android:layout_height="fill_parent" android:layout_width="fill_parent" /> <TextView android:id="@+id/list_empty" android:text="No Item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> </LinearLayout> 

Then check the number of visibility elements on the ListView symbol, respectively:

  ListView lv = (ListView)findViewById(R.id.list); lv.setVisibility((adapter.isEmpty())?View.GONE:View.VISIBLE); 

If you use the Custom Adapter, you can do this in the overridden notifyDataSetChanged method.

+9
Apr 6 2018-11-11T00:
source share

You can use Toast Message for this.

Check the value of Count of the Adapter on adapter.getCount()

 if(Adapter.getCount()!=0){ List.setAdapter(Adapter); }else{ Toast.makeText(YourActivityName.this, "No Items Available",Toast.LENGTH_SHORT).show(); } 
+4
Apr 6 2018-11-11T00:
source share

For Josephโ€™s layout code, you need to edit @ + id / list and @ + id / empty for @android: id / *, for example:

 <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <TextView android:id="@android:id/empty" android:text="Empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" /> 

That way, you donโ€™t even have to override the onContentChanged () function.

+1
Jan 24 '13 at 6:28
source share

The easiest way to achieve this is to use ListFragment instead of ListActivity. ListFragment has the following convenient method:

 setEmptyText("My no items message..."); 

In addition, using the ListFragment class has other advantages. For example, the ability to combine it with the new AppCompat library (which you cannot do with ListActivity because you need to switch from ActionBarActivity).

+1
Jul 29 '13 at 23:47 on
source share



All Articles