Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android ListActivity 用法實戰:免寫 setContentView 的清單頁面與動態選單切換

編輯:關於 Android 程式設計

如果你只是要顯示一份清單,那麼 ListActivity 往往比一般 Activity 更省事。它把 ListView 的初始化與資料綁定都包好了,你只要交出一個 Adapter 就能開工。本文用一個含雙選單切換的小範例,說明它的特性與適用場合。

一、ListActivity 的定位

ListActivity 屬於專門用來承載清單的 Activity 型別,常見應用包括投票選項、多項目條列、檔案瀏覽器與各類選單明細。它最大的便利在於:不必像 Activity 那樣非得呼叫 setContentView 設定版面,甚至可以在不去改寫 onCreate 的情況下直接把清單掛上去。

二、範例目標

本範例在 ListActivity 中建置兩個 Options Menu,點選不同選單時動態替換清單內容;同時改寫 onListItemClick() 以攔截使用者的點擊動作,並用 Toast 做最簡便的回饋提示。

三、程式碼

 import android.app.ListActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class A08Activity extends ListActivity { private int selectedItem=-1; private ArrayAdapter<String> aa; private String[] s; private static final int MENU_LIST01=Menu.FIRST; private static final int MENU_LIST02=Menu.FIRST+1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); } protected void onListItemClick(ListView l,View v,int position,long id){ selectedItem=position; Toast.makeText(A08Activity.this, s[selectedItem], Toast.LENGTH_LONG).show(); super.onListItemClick(l, v, position, id); } public boolean onCreateOptionsMenu(Menu menu){ int idGroup1=0; int orderMenuItem01=Menu.NONE; int orderMenuItem02=Menu.NONE+1; menu.add(idGroup1, MENU_LIST01, orderMenuItem01, R.string.str_menu_list01); menu.add(idGroup1, MENU_LIST02, orderMenuItem02, R.string.str_menu_list02); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){ case MENU_LIST01: s=new String[]{ getResources().getString(R.string.str_list01), getResources().getString(R.string.str_list02), getResources().getString(R.string.str_list03), getResources().getString(R.string.str_list04) }; aa=new ArrayAdapter<String>(A08Activity.this,R.layout.main,s); A08Activity.this.setListAdapter(aa); break; case MENU_LIST02: s=new String[]{ getResources().getString(R.string.str_list05), getResources().getString(R.string.str_list06), getResources().getString(R.string.str_list07), getResources().getString(R.string.str_list08) }; aa=new ArrayAdapter<String>(A08Activity.this,R.layout.main,s); A08Activity.this.setListAdapter(aa); break; } return super.onOptionsItemSelected(item); } } 

四、項目版面:main.xml

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/hello" /> 

五、幾個實作重點

一是清單項目的版面直接沿用 R.layout.main(此例即一個 TextView),Adapter 會把它當成每一列的樣板;二是 setListAdapter() 可以在執行期反覆呼叫,因此選單切換本質上就是「換一份資料、換一個 Adapter」;三是改寫 onListItemClick() 時別忘了呼叫父類別實作,以保留內建行為。

更多 Android 組件與 Activity 操作技巧,可參考本站《Android 開發入門與進階教程》與《Android 編程之 Activity 操作技巧總結》專題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved