【问题标题】:Issue using setEmptyView on a ListActivity在 ListActivity 上使用 setEmptyView 的问题
【发布时间】:2010-03-07 17:46:02
【问题描述】:

我有以下设置,但没有显示空视图文本...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            getListView().setEmptyView(findViewById(R.id.empty));
    getListView().setTextFilterEnabled(true);

    setListAdapter(null);// to have empty list
}

布局文件为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/myScrollLayout"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
    <FrameLayout android:layout_width="fill_parent"
        android:layout_height="0dip" android:layout_weight="1">
        <ListView android:id="@android:id/list" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:drawSelectorOnTop="false" />

        <!-- Here is the view to show if the list is emtpy -->
        <ScrollView
            android:id="@android:id/empty" android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView android:id="@+id/emptyText" android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:text="@string/contact_empty_help"
                android:textSize="20sp" android:textColor="?android:attr/textColorSecondary"
                android:paddingLeft="10dip" android:paddingRight="10dip"
                android:paddingTop="10dip" android:lineSpacingMultiplier="0.92" />
        </ScrollView>
    </FrameLayout>
</LinearLayout>

字符串定义为:

<string name="contact_empty_help">"You don't have any contacts to display.\n\nTo add contacts, you have to:\n
        \n<li><font fgcolor="#ffffffff">Go to <b>Contacts</b></font> application to create new contacts\n</li>
        \n<li><font fgcolor="#ffffffff"><b>Import from VCF file</b></font>, this is available in Contacts menu, the file most be on the root of the SD card\n</li>"
        </string>

由于列表为空,它应该向我显示定义的文本。我遵循了 List8 中的示例

https://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List8.html
https://developer.android.com/resources/samples/ApiDemos/res/layout/list_8.xml

【问题讨论】:

    标签: android


    【解决方案1】:

    我错过了 setContentView(R.layout.listview);,因此 findViewById 没有获取视图。

    【讨论】:

      【解决方案2】:

      其实你的java代码中不需要这行

      getListView().setEmptyView(findViewById(R.id.empty));

      Android ID android:id="@android:id/list"android:id="@android:id/empty"独家

      最好的问候, 除法零。

      【讨论】:

      • 我认为这仅在 ListActivitys 中是正确的,我刚刚从我的代码中删除了这一行,emptyView 出现在填充列表的顶部。
      【解决方案3】:

      不应该

      <ScrollView
              android:id="@android:id/empty"
      

      成为

      <ScrollView
              android:id="@+id/empty"
      

      ?我不知道这有什么不同。

      你确定setListAdapter(null);实际上是在初始化适配器,而不是仅仅忽略命令?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多