【问题标题】:Showing empty view when ListView is emptyListView 为空时显示空视图
【发布时间】:2011-04-15 20:30:05
【问题描述】:

由于某种原因,即使 ListView 不为空,空视图(在这种情况下为 TextView)也会始终出现。我认为 ListView 会自动检测何时显示空视图。

<RelativeLayout android:id="@+id/LinearLayoutAR"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <ListView android:id="@+id/ARListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></ListView>
    <ProgressBar android:id="@+id/arProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"></ProgressBar>
    <!-- Here is the view to show if the list is emtpy -->
    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="No Results" />
</RelativeLayout>

如何正确连接空视图?

【问题讨论】:

    标签: android listview


    【解决方案1】:

    当您扩展 FragmentActivityActivity ListActivity时,您会想看看:

    ListView.setEmptyView()

    【讨论】:

    • 知道如何在应用发明者中做到这一点吗?
    【解决方案2】:

    应该是这样的:

    <TextView android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="No Results" />
    

    注意 id 属性。

    【讨论】:

    • 您必须使用 ListViewActivity 才能以这种方式工作。
    • 哦,我明白了,我的 ListView 实际上是在 ViewFlipper 中。当列表为空时,还有其他方法可以呈现视图吗?
    • 你自己实现了逻辑,看看 ListViewActivity 的源代码看看他们是怎么做的,或者当你做查询时检查它是否为空,如果是的话,设置你的文本视图为可见,否则将其设置为 GONE
    • 在使用自定义适配器时使用 textview 不起作用。
    • @schwiz 错误!你不需要 ListViewActivity 来使用空功能。
    【解决方案3】:

    正如appsthatmatter所说,在布局中是这样的:

    <ListView android:id="@+id/listView" ... />
    <TextView android:id="@+id/emptyElement" ... />
    

    并在链接的活动中:

    this.listView = (ListView) findViewById(R.id.listView);
    this.listView.setEmptyView(findViewById(R.id.emptyElement));
    

    也适用于 GridView...

    【讨论】:

    • 经过至少一个小时的尝试,但没有任何效果(在列表视图中显示“空”文本以及数据),这对我来说在 Activity 中使用 ListView 时有用扩展 ListActivity。谢谢你,艾迪!
    • 我应该隐藏 TextView 还是什么?
    • @Harsha android:visibility="gone"
    【解决方案4】:

    我尝试了上述所有解决方案。我想出了解决问题。在这里我发布完整的解决方案。

    xml 文件

    <RelativeLayout
        android:id="@+id/header_main_page_clist1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:paddingBottom="10dp"
        android:background="#ffffff" >
    
        <ListView
            android:id="@+id/lv_msglist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@color/divider_color"
            android:dividerHeight="1dp" />
    
        <TextView
            android:id="@+id/emptyElement"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="NO MESSAGES AVAILABLE!"
            android:textColor="#525252"
            android:textSize="19.0sp"
            android:visibility="gone" />
    </RelativeLayout>
    

    textView ("@+id/emptyElement") 是空列表视图的占位符。

    这是java页面的代码:

    lvmessage=(ListView)findViewById(R.id.lv_msglist);
    lvmessage.setAdapter(adapter);
    lvmessage.setEmptyView(findViewById(R.id.emptyElement));
    

    记得在将适配器绑定到 listview 后放置 emptyView。我的第一次没有工作,在我将 setEmptyView 移到 setAdapter 之后它现在可以工作了。

    输出:

    【讨论】:

    • 在为 emptyElement 添加 android:layout_below="@+id/lv_msglist" 后,这对我有用
    【解决方案5】:

    我强烈推荐你像这样使用 ViewStubs

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
        <ViewStub
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout="@layout/empty" />
    </FrameLayout>
    

    查看Cyril Mottier的完整示例

    【讨论】:

      【解决方案6】:
      <ListView android:id="@+id/listView" ... />
      <TextView android:id="@+id/empty" ... />
      and in the linked Activity:
      
      this.listView = (ListView) findViewById(R.id.listView);
      this.listView.setEmptyView(findViewById(R.id.empty));
      

      如果您使用的是支持库,这可以与 FragmentActivity 一起使用。通过构建 API 17(即 4.2.2 映像)对此进行了测试。

      【讨论】:

        【解决方案7】:

        活动代码,扩展ListActivity很重要。

        package com.example.mylistactivity;
        
        import android.app.ListActivity;
        import android.os.Bundle;
        import android.widget.ArrayAdapter;
        import com.example.mylistactivity.R;
        
        // It's important to extend ListActivity rather than Activity
        public class MyListActivity extends ListActivity {
        
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.mylist);
        
                // shows list view
                String[] values = new String[] { "foo", "bar" };
        
                // shows empty view
                values = new String[] { };
        
                setListAdapter(new ArrayAdapter<String>(
                        this,
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1,
                        values));
            }
        }
        

        布局xml,两个视图中的id都很重要。

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        
            <!-- the android:id is important -->
            <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
        
            <!-- the android:id is important -->
            <TextView
                android:id="@android:id/empty"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="i am empty"/>
        </LinearLayout>
        

        【讨论】:

          【解决方案8】:

          只是补充一点,您实际上并不需要创建新的 ID,类似以下的方法就可以了。

          在布局中:

          <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="wrap_content"
              android:id="@android:id/empty"
              android:text="Empty"/>
          

          然后在活动中:

              ListView listView = (ListView) findViewById(android.R.id.list);
              listView.setEmptyView(findViewById(android.R.id.empty));
          

          【讨论】:

            【解决方案9】:

            我遇到了这个问题。我必须让我的类扩展 ListActivity 而不是 Activity,并将 XML 中的列表重命名为 android:id="@android:id/list"

            【讨论】:

              【解决方案10】:

              以编程方式解决方案是:

              TextView textView = new TextView(context);
              textView.setId(android.R.id.empty);
              textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
              textView.setText("No result found");
              listView.setEmptyView(textView);
              

              【讨论】:

                【解决方案11】:

                首先检查列表是否包含一些值:

                if (list.isEmpty()) {
                    listview.setVisibility(View.GONE);
                }
                

                如果是则OK,否则使用:

                else {
                     listview.setVisibility(View.VISIBLE);
                }
                

                【讨论】:

                • 您是否愿意进一步详细说明list 是什么?
                • 这是一个数组列表
                • Object[] response = Utility.parseStationresponce(object); int statusCode = (Integer) response[0]; @SuppressWarnings("unchecked") ArrayList list = (ArrayList) response[2];
                • 很好,我建议您将其作为答案的一部分而不是 cmets。这对于让其他人理解你所写内容的具体背景很重要。此外,最好将您的答案与 OP 的问题联系起来。最后,欢迎来到 SO。
                猜你喜欢
                • 1970-01-01
                • 2019-11-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多