編輯:關於 Android 程式設計
做小練習時又碰到 Adapter,才發現自己一直沒好好整理過它。這次就用最基礎的 ArrayAdapter 為例,把整個資料流走一遍。
一句話:Adapter 是資料與視圖之間的橋樑。資料在 Adapter 這層被整理成型,再由 ListView 逐行取出、貼到版面配置的視圖上。
Android 內建的 Adapter 家族相當龐大,常見的有 ArrayAdapter、BaseAdapter、CursorAdapter、SimpleAdapter、SimpleCursorAdapter、ResourceCursorAdapter、HeaderViewListAdapter、SpinnerAdapter、WrapperListAdapter 等。其中 BaseAdapter 是最靈活的基底,ArrayAdapter 則是最容易上手的起點。
上半部是輸入區(姓名、地址與一顆儲存鈕),下半部留給 ListView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address:" />
<EditText
android:id="@+id/addr"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
public class LunchList extends Activity {
List<Restaurant> model = new ArrayList<Restaurant>();
ArrayAdapter<Restaurant> adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView) findViewById(R.id.restaurants);
adapter = new ArrayAdapter<Restaurant>(this,
android.R.layout.simple_list_item_1, model);
list.setAdapter(adapter); // 把配好的適配器交給 ListView
}
private View.OnClickListener onSave = new View.OnClickListener() {
public void onClick(View v) {
Restaurant r = new Restaurant();
r.setName(((EditText) findViewById(R.id.name)).getText().toString());
r.setAddress(((EditText) findViewById(R.id.addr)).getText().toString());
RadioGroup types = (RadioGroup) findViewById(R.id.types);
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down: r.setType("sit_down"); break;
case R.id.take_out: r.setType("take_out"); break;
case R.id.delivery: r.setType("delivery"); break;
}
adapter.add(r); // 每次新增的項目都進到適配器裡
}
};
}
1. Adapter 的角色:資料與視圖之間的橋樑, ListView 自己不碰資料,全部透過 Adapter 取得。
2. 泛型參數:ArrayAdapter<Restaurant> 表示這個適配器管理的是 Restaurant 物件陣列。泛型也可以是字串、數字或任何自訂類別。
3. 三個建構參數:
adapter = new ArrayAdapter<Restaurant>(this,
android.R.layout.simple_list_item_1, model);
adapter.add(r) 一筆筆補進去。4. 整體流程:ListView 拿到這三個資訊後,會遍歷資料集合,每讀出一筆就 inflate 一次指定版面並填入內容,逐列堆疊成我們看到的清單。
ArrayAdapter 只會呼叫 TextView 的 setText(),一旦列上要放圖片或多個欄位,就得換成繼承 BaseAdapter 自訂 getView()。那時你會遇到另一個重要課題:複用 convertView 與ViewHolder 模式,這才是清單捲動順不順的關鍵。另外在現今專案中,ListView 多已讓位給 RecyclerView,但 Adapter 的核心概念是完全相通的。
【React Native 開發】ProgressBarAndroid 進度條組件講解(12)
(一)前言 今天我們一起來看一下進度加載條 ProgressBarAndroid 控件的講解與基本使用。 該 ProgressBarAndroid 的 React 組件封
使用 Chrome 瀏覽器調試 Android App 詳解
我一直對 Chrome 情有獨鐘,它不僅速度更快,對開發者也格外友好。內置強大的 Developer Tools 讓 Web 開發者愛不釋手,Chrome 商店裡五花八門
MESUIT機甲安裝使用教學 讓iPhone多用一張卡
雙卡、雙電、雙系統——這三項對商務人士與重度用戶而言簡直是天賜福音。機甲正是這樣一款高端智能外設,它讓你同時使用iOS與Android,並以組合創新的方式補足iPhone
Android 自定義 ViewPager 滑動行為:控制左右切換與頁面顯示
為何要自定義 ViewPager 滑動 標準 ViewPager 預設允許左右滑動切換頁面,但實際專案常有特殊需求。例如某個頁面需要禁止向右滑動,或第三個頁面只能透過按鈕