【发布时间】:2012-10-22 13:42:20
【问题描述】:
我正在使用 ListView,如果列表为空,我会显示 EmptyView。如何检查此视图是否显示为布尔变量?
这里是我的 ListView 的代码:
deviceList = (ListView) findViewById(R.id.DeviceList);
ListAdapter listenAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_2, deviceArray);
//add a view which is shown if the ListView is empty
deviceList.setEmptyView( findViewById( R.id.empty_list_view ) );
deviceList.setAdapter(listenAdapter);
【问题讨论】:
-
看看
ListView的适配器是null还是里面有值。 -
我该怎么做? ListAdapter 没有这样的方法,这里是Android开发者页面的链接developer.android.com/reference/android/widget/ListAdapter.html
-
您使用超类(在这种情况下为接口),但您的适配器是
ArrayAdapter,所以使用它。示例:boolean isEmptyViewShown = ((ArrayAdapter) deviceList.getAdapter() == null) || (((ArrayAdapter) deviceList.getAdapter()).getCount() == 0); -
我尝试了您的代码,但即使我将项目添加到列表中,布尔变量也始终为真。你知道为什么吗?
-
您必须在 将项目添加到列表后运行该代码(或在需要了解视图状态时运行它),即
boolean变量不会自行更新。
标签: android listview listadapter