2011-10-05 66 views
-3

如何爲以下XML找到ViewById?在Android中查找ListView

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

我需要它來設置一個列表適配器。

+0

我不知道這是否是正確的語法爲一個id *(不知道冒號是否允許在id名稱內)*。通常這些列表id是'@android:id/list' *(例如在Preference-或者ListActivity中)*,或者@ @ id/list'用於你自己的ID。 – 2011-10-05 18:01:38

+0

這個問題很糟糕,因爲它顯示海報部分的研究很少。快速谷歌搜索會找到結果。 –

回答

0

這個基本的例子展示瞭如何在一個ListView中使用ArrayAdapter。你應該真的需要實際上混淆ListView本身,只是放入它的元素。

http://developer.android.com/resources/tutorials/views/hello-listview.html

+0

在這裏更有建設性:答案應該是可以理解的,沒有鏈接。總結鏈接的重要部分,並用它爲OP提供一個研究更多的地方。爲什麼?因爲鏈接可能暫時停止或根本不存在*(即使官方文檔,請參閱kernel.org當前的災難)*。記住,你不僅給OP一個答案,你還幫助一些隱形的搜索引擎用戶。如果您只想發佈鏈接,請將其作爲評論發佈。 – 2011-10-05 18:09:27

+0

@alextsc足夠公平 –

+0

@KurtisNusbaum你應該編輯你的答案,以提供更多的信息,而不是說「夠公平」。 – Merlin

0

findViewById(android.R.id.list)

android:前綴的ID都android.R.id下訪問。同樣@android:drawableandroid.R.drawable等訪問...

1

你的XML必須是這樣的:

<ListView android:id="@+id/ListView_Name" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 
在你的代碼

ListView listview = (ListView) findViewById(R.id.ListView_Name); 
+0

請注意格式。反引號用於內聯突出顯示。突出顯示代碼並單擊大括號以格式化代碼塊;) – Merlin

+0

謝謝@Merlin;) –

+0

+1給出正確答案,但OP未能識別您的努力 – Merlin