简体   繁体   中英

How to set EmptyView in ListActivity

I would like to display Empty View like " No Items Found " in ListActivity if it is Empty. I have searched more on sites and all suggestions for setEmptyView method of ListView only. Can you please guide for ListActivity?

You can use the getListView().setEmptyView(yourTextView); method in ListActivity.

TextView emptyView = new TextView(mContext);
emptyView.setText("Empty List");
emptyView.setTextSize(25);
emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

getListView().setEmptyView(emptyView);

Edit

I just found that Cyril Mottier wrote a blog about this. It seems that it is exactly what you would like to do.

I have found the solution as below

TextView emptyview = new TextView(this);
emptyview.setText("Empty List");
emptyview.setTextSize(25);

ListView currentlistview = getListView();

if(listadapter.size()==0)
((ViewGroup)currentlistview.getParent()).addView(emptyview);

I have to search more for the solution... ;)

For add an empty view for ListActivity according too source code :

  1. Create a xml layout, for example R.layout.list_activity , with ListView and TextView with identifiers (required!) android:id/list , android:id/empty .

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" > <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/list" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/empty" android:text="@string/empty" android:gravity="center" /> </LinearLayout>
  2. Set this layout:

     @Override protected void onCreate( Bundle savedInstanceState ){ super.onCreate( savedInstanceState ); setContentView( R.layout.list_activity ); //... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM