Android ListView无数据时显示其他View

大家都遇到过这样的情况,当 ListView 中的数据为空时,需要显示一个 “没有数据” 的 View 。想过一些办法,比如 FrameLayout 来 包含 ListView 和 数据为空时的 View,当有数据时隐藏空 View,没有数据时显示空 View。或者是给 ListView 添加一个 header 或 footer。但是今天看到了一种更简单的方案,下面来介绍一下。

先贴一下今天看到的代码

布局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No items." />

</FrameLayout>

Activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, generateStrings());

setListAdapter(adapter);

}

private String[] generateStrings() {
String[] strings = new String[0];

for (int i = 0; i < strings.length; ++i) {
strings[i] = "String " + i;
}

return strings;
}

}

Activity 是继承自 ListActivity ,到这里代码都能看懂。奇怪的一点是布局文件中 android:id="@android:id/empty" id 是这样写的。在网上查了一下,这个属性的作用就是,当 ListView 关联的 Adapter 的数据为时,就显示这个 id 为 @android:id/empty 的 View。而当数据不为空时,这个 空 View 就不可见。

如果在不是继承自 ListActivity 的话,上面的那个属性没有作用,TextView 会是一直可见的状态。

所以如果是不继承 ListActivity 的情况下,需要在代码中调用 listView.setEmptyView(nullView) 来设置 listView 数据为空时 显示 nullView 。

样例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<!-- Here is the view to show if the list is emtpy -->

<TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No items." />

</FrameLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class MainActivity extends Activity {

private ListView mListView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mListView = (ListView) findViewById(R.id.myList);
mListView.setEmptyView(findViewById(R.id.myText));

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, generateStrings());

mListView.setAdapter(adapter);
}

private String[] generateStrings() {
String[] strings = new String[100];

for (int i = 0; i < strings.length; ++i) {
strings[i] = "String " + i;
}
return strings;
}
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×

keyboard_arrow_up 回到顶端