Show TextView for an empty ListView inside a FrameLayout

前端 未结 4 1899
我寻月下人不归
我寻月下人不归 2021-01-28 09:18

I have a tabbed layout and an activity using tabs as views. It has three tabs as ListViews. If either of the lists is empty I want to show a simple TextView

4条回答
  •  无人共我
    2021-01-28 09:26

    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(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.

提交回复
热议问题