編輯:關於 Android 程式設計
ListView 是 Android 早期最常使用、也最容易被誤解的元件之一。它的使用流程固定由三個部分組成:準備資料、建立 Adapter、把資料綁上 ListView。而在入門階段,幾乎每個人都會撞上一個經典錯誤:
Your content must have a ListView whose id attribute is "android.R.id.List"
網路上流傳的解法是「把 android:id="@+id/list" 改成 android:id="@+id/android:list"」。這確實能消除報錯,但它其實不是原因,只是結果——真正的原因是:你採用的使用方式與程式碼寫法不匹配。
這種寫法讓 Activity 本身繼承 ListActivity,系統會自動尋找 id 為 @android:id/list 的 ListView 並與之關聯。此時:
@android:id/list(也可寫 @+id/android:list)。findViewById(),而是直接呼叫 setListAdapter()。主版面:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical" />
</LinearLayout>
Java 端:
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 注意:此處不用 findViewById 取得 ListView
ArrayList<HashMap<String, String>> list = new ArrayList<>();
HashMap<String, String> map1 = new HashMap<>();
map1.put("key", "張三");
map1.put("value", "學生");
HashMap<String, String> map2 = new HashMap<>();
map2.put("key", "李四");
map2.put("value", "老師");
list.add(map1);
list.add(map2);
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.hashmap,
new String[]{"key", "value"},
new int[]{R.id.key, R.id.value});
setListAdapter(adapter);
}
}
若 Activity 繼承的是普通的 Activity,系統不會幫你尋找任何特定 id 的 ListView,這時:
@+id/list 即可,叫什麼都可以。findViewById() 取得 ListView,再呼叫 setAdapter()。listView = (ListView) findViewById(R.id.list); listView.setAdapter(adapter);
資料準備與 SimpleAdapter 的建構方式與前一個例子完全相同。
把兩種方式並排看就很清楚了:那個錯誤訊息只會出現在你繼承了 ListActivity,卻在版面中找不到 @android:id/list 的情況。如果你的 Activity 繼承的是 Activity,就算把 id 寫成 @+id/list 也不會報這個錯。
因此排查順序應該是:
ListActivity 還是 Activity。@android:id/list。findViewById() 取得 ListView 並呼叫 setAdapter()。兩種方式共用同一份 item layout,這裡用水平排列的兩個 TextView 示範:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/linelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#aa0000" />
<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
SimpleAdapter 的最後兩組陣列就是「資料中的 key 名稱」與「要填入的 View id」的一一對應表,這也是為什麼 item layout 中的 id 必須和 new int[]{...} 完全一致。
釐清這層關係之後,ListView 的入門門檻其實不高。真正複雜的議題(View 回收、ViewHolder 優化、多類型項目)則要等熟悉基本綁定流程之後再來處理。
ListView 與 ExpandableListView 實戰:多選、單選、全選與批量輸入
專案裡常會遇到這類需求:列表要能勾選、要能全選、要在同一行裡做單選,甚至每一列都要填幾個數字並即時加總。功能本身都不算難,真正的坑在列表項視圖復用導致的狀態錯亂。這一篇把
自製仿 MIUI 風格 ViewPagerIndicator:Tab 與頁面滑動聯動的完整實作
一、要做什麼 做過 Tab 切換的人大概都用過 TabPageIndicator,許多知名 App 也採用這個開源指示器。但你有沒有想過自己動手做一個?究竟有多複雜?這篇
Android 自訂 View 實現照片裁剪框與裁剪功能
本文要實作的是一種頗具質感的照片裁剪效果: 右上角放了個裁剪框,按下確定後,就能把方框內的正方形區域裁下來,並顯示在右上角。 實現思路 先自訂一個 ZoomImag
Android 懸浮窗權限適配全景:各品牌 ROM 與各版本的處理方式總結
懸浮窗權限是 Android 碎片化最典型的痛點之一。這篇文章把主流品牌、各個系統版本的適配路徑整理成一份可查的參考。必須先說明:由於 ROM 變體實在太多,任何一份清單