setEmptyView on ListView not showing its view in a android app?

前端 未结 12 773
自闭症患者
自闭症患者 2020-12-02 18:37

I have a problem with the setEmptyView method from a ListView.

Here is my Java code:

ListView view = (ListView)findViewById(R.id.listView1);
view.se         


        
相关标签:
12条回答
  • 2020-12-02 19:10

    After trying all of the solutions presented here and experimenting, I believe the code below is the best method to use when your ListView and empty View are not side-by-side in a layout with the automatic ids set.

    ViewGroup parentGroup = (ViewGroup)getListView().getParent();
    View empty = getActivity().getLayoutInflater().inflate(R.layout.list_empty_view,
                                                           parentGroup,
                                                           false);
    parentGroup.addView(empty);
    getListView().setEmptyView(empty);
    

    Reasoning:

    • By passing the parent group into inflate, the layout_* attributes of your empty view layout are respected
    • By not attaching the view in inflate (the false parameter), the empty view is returned. Otherwise, inflate would return the parent requiring us to use parentGroup.findViewById(empty_view_id) to get the empty view for use with setEmptyView(). Here we avoid the extra lookup, the need for another id, and the need to expose that id in our code. If we didn't need to preserve the reference to empty, telling inflate to attach would be the correct action removing the need for the addView call.
    • An anti-pattern, addContentView(), is not the correct approach. It will appear equivalent as long as your ListView occupies all of your Activity's visible space. Instead of your empty view being a sibling to your ListView, it ends up being a sibling to your other root-level layouts in your Activity. It appears to work because it's floating (z-order) on top of all other Views in the activity. See: Activity.addContentView(View) == ViewGroup.addContentView(View)?
    0 讨论(0)
  • 2020-12-02 19:14

    Create a layout in XML that specifies both the ListView and the empty view you want to use. If you give them IDs of @android:id/list and @android:id/empty, android will automatically use the view with id:empty when list is empty.

    0 讨论(0)
  • 2020-12-02 19:15

    the problem is, i have to do a addContentView:

    View empty = getLayoutInflater().inflate(R.layout.empty_list_item, null, false);
    addContentView(empty, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    view.setEmptyView(empty);
    

    now its fine

    0 讨论(0)
  • 2020-12-02 19:15

    For me, none of this answers worked for me. What I had to do was add the empty view manually (inflated) to the "parent" view of the Listview:

    ListView my_list = (ListView) findViewById(R.id.my_list);
    View emptyView = getLayoutInflater().inflate(R.layout.empty_view,null);
    ((ViewGroup)my_list.getParent()).addView(emptyView); 
    listView.setEmptyView(emptyView);
    
    0 讨论(0)
  • 2020-12-02 19:15

    My problem was, that I added a footer view which seems not working with the empty view. After removing the footer view the empty view is displayed.

    0 讨论(0)
  • 2020-12-02 19:17

    Your TextView should be placed right under the ListView item with its visibility set to gone (android:visibility="gone"), do not place it in another layout. This is how your main layout would look like

    <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="@+id/listViewFangbuch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
        <TextView
            android:id="@+id/empty_list_item"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:text="@string/emptyList" >
        </TextView>
    
    </LinearLayout>
    

    And this is how your code might look like

    ListView view = (ListView)findViewById(R.id.listViewFangbuch);
    view.setEmptyView(findViewById(R.id.empty_list_item));
    
    ArrayAdapter<Session> adapter1 = new ArrayAdapter<Session>(this, android.R.layout.simple_list_item_1, 
            android.R.id.text1, MainController.getInstanz().getItems());
    view.setAdapter(adapter1);
    
    0 讨论(0)
提交回复
热议问题