2012-10-22 34 views
1

我正在使用一個ListView,如果列表爲空,我將顯示EmptyView。我如何檢查這個視圖是否顯示一個布爾變量?如何檢查我的ListView EmptyView是否顯示在Android中?

這裏我的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); 
+0

查看ListView的適配器是否爲null或者它是否有任何值。 – Luksprog

+0

我該怎麼做? ListAdapter沒有這樣的方法,這裏的鏈接t Android開發者頁面http://developer.android.com/reference/android/widget/ListAdapter.html –

+1

你使用超類(在這種情況下的接口),但你的適配器是一個'ArrayAdapter'使用它。例如:'布爾isEmptyViewShown =((ArrayAdapter)deviceList.getAdapter()== NULL) \t \t \t \t || (((ArrayAdapter)deviceList.getAdapter())。getCount()== 0);' – Luksprog

回答

0

您可以使用 「特殊」 的Android ID的XML做到這一點。如果您在您的佈局xml文件中使用此代碼的空文本需要時會自動顯示:

<ListView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@id/android:list"/> 
<TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/no_tasks" android:id="@id/android:empty" 
android:gravity="center_vertical|center_horizontal"/> 

與Android TextView的:列表組件是空的:當Android的空ID只顯示。

我希望有幫助。

+0

這不是我的問題,我已經能夠顯示空的列表。但是我想要一個布爾變量,如果顯示空列表,它的值爲「真」 –

+0

啊,的確,道歉。我需要RTFQ ;-) – Broo

相關問題