Show TextView for an empty ListView inside a FrameLayout

戏子无情 提交于 2019-12-02 11:53:16

Just call setEmptyView(...) on your ListView, passing in the TextView as argument.

TextView empty = (TextView) findViewById(R.id.blank);
mListView_top10.setEmptyView(empty);

The ListView should automatically take care of toggling the visibility of the 'empty' view.

Tip: since the argument is a generic View, you can pass in any subclass of View, or more complex view hierarchy.

On a side note: you will probably have to give each ListView its own TextView instance as empty view to avoid clashing scenarios; e.g. when one list does have content, while another doesn't.

I would insist you to use ViewStub here with a ListView inside a FrameLayout. When your ListView has data you can use VIEW.GONE to ViewStub and if your ListView is no data then use VIEW.VISIBLE for the ViewStub. You can download example from github and get it working.

You can solve this by adding an empty item when the list is empty. The following example is based on your pre-EDIT code:

FrameLayout frameLayout = (FrameLayout) findViewById(android.R.id.tabcontent);

mListView_top10 = (ListView)findViewById(R.id.Top_10);
if (TopWR.size() == 0) {
  // add a single item to the array, in order to indicate the list is empty
  TopWR.add("The List Is Empty");
}
mListView_top10.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_row,TopWR));

If you want to get fancier with how the empty list is displayed you can replace ArrayAdapter with a custom adapter. You can also use a different adapter when the list is empty.

Have you tried setting the Visibility on each of the ListViews in your java code?

TextView empty = (TextView) findViewById(R.id.blank);
FrameLayout frameLayout = (FrameLayout) findViewById(android.R.id.tabcontent);


mListView_top10 = (ListView)findViewById(R.id.Top_10);
      if(TopWR.size()!=0) {
          mListView_top10.setVisibility(View.VISIBLE);
          mListView_top10.setAdapter(new ArrayAdapter<String>(this,
                    R.layout.listview_row,TopWR));


      }
      else {
          mListView_top10.setVisibility(View.GONE);
          empty.setVisibility(View.VISIBLE);
          frameLayout.addView(empty);

      }

TextView

<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/blank"
        android:visibility="gone"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="No records Avaible"
        android:textColor="#ffffff" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!