ArrayAdapter & Spinner
Adapter in Android Android應用了adapter的概念來橋接「資料」與「介面元件」 Adapting: 當兩個不同介面的物件必須互通時,由adapter負責介面之間的橋接 (adapter pattern) 例如:當字串陣列要作為下拉式選單的item,android要求使用adapter來轉接陣列與選單 Android adapter種類有:ArrayAdapter, SimpleAdapter, CursorAdapter 本次課程主要探討ArrayAdapter與下拉式選單
ArrayAdapter (陣列接口) ArrayAdapter<String> adapter = new ArrayAdapter<String>(arg1, arg, arg3):新增一個陣列接口 Arg1: this activity (以this) Arg2: 接口樣式的識別符號 Arg3: 傳入接口的陣列
使用ArrayAdapter建構一個下拉式選單的view Adapter.setDropDownViewResource(int res) Sets the layout resource to create the drop down views
範例 private String names[]={"stewart", "mary", "john"}; //宣告並初始化一個字串陣列 ….. ArrayAdapter<String> adapter_name=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, names); //初始化一個名為adapter_name的陣列接口 adapter_name.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //將字串陣列橋接至下拉式選單的view中
將陣列獨立成專案的資源 若字串陣列用於作為下拉式選單的「選項」,那麼可以將選項直接獨立成為string.xml所記載的「資源」,而不須成為.java的程式碼之中 res/valuse/string.xml <resources> ……. <string-array name=“students”> //定義一個名為students的陣列資源 <item>Stewart</item> <item>Mary</item> <item>John</item> </string-array> </resources>
從專案「資源」中建立ArrayAdapter ArrayAdapter<CharSequence> . createFromResource(this, resID, viewResID) Creates a new ArrayAdapter from external resources 僅用於<CharSequence>,且需指定橋接目標的viewID ArrayAdapter<CharSequence> adapter_name=ArrayAdapter.createFromResource(this, R.array.students, android.R.layout.simple_spinner_item);
Spinner – 下拉式選單
main.xml中新增spinner Spinner – 下拉式選單介面元件 drawSelectorOnTop (必須): prompt: spinner的標題。該屬性不接受string,必須透過外部資源 <Spinner android:id="@+id/xxx" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="true" android:prompt="@string/color_prompt" />
android:prompt 指定下拉式選單標題時,必須在外部資源string.xml中先制定一個<string>資源,再指定到android:prompt <resources> … <string name="color_prompt">字串前景顏色</string> </resources>
練習 試著設計一個具備「Stewart」、「Mary」、「John」三位學生姓名的下拉式選單 string.xml 中新增代表prompt以及<string-array> main.xml中新增<spinner>介面元件 .java程式中透過ArrayAdapter接口,將string-array外部資源橋接至spinner
新增外部字串資源 Res/values/string.xml <resources> … <string name="students">學生列表</string> <string-array name="student_name"> <item>Stewart</item> <item>Mary</item> <item>John</item> </string-array> </resources>
設計spinner元件 main.xml: <Spinner android:id="@+id/foreColor" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="true" android:prompt="@string/students" />
宣告並設定ArrayAdapter private Spinner field_name; private TextView txtDisplay; private void findViews(){ field_name=(Spinner)findViewById(R.id.foreColor); txtDisplay=(TextView)findViewById(R.id.txtDisplay); ArrayAdapter<CharSequence> adapter_name=ArrayAdapter.createFromResource(this, R.array.student_name, android.R.layout.simple_spinner_item); adapter_name.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); field_name.setAdapter(adapter_name); } //此時可以開啟模擬器欣賞下拉式選單的外觀,但是還未完成點選後的動作
Spinner的「動作」 下拉式選單的動作不外乎就是「點選」選項 隸屬於spinner.onItemSelectedListener onNothingSelected(): 通常不實作 隸屬於spinner.onItemSelectedListener Spinner.setOnItemSelectedListener()
修改setListeners() private void setListeners(){ field_name.setOnItemSelectedListener(getName); }
新增一個OnItemSelectedListener private Spinner.OnItemSelectedListener getName=new Spinner.OnItemSelectedListener(){ public void onItemSelected(AdapterView<?> adapterView, View v, int position, long id) { txtDisplay.setText("Hello, my name is " + adapterView.getSelectedItem().toString()); } public void onNothingSelected(AdapterView<?> arg0) { };
Get selected item and position Spinner可以取得所點選的「選項」以及選項在列表中的位置號碼(以0開始) .getSelectedItem():傳回選項 .getSelectedItemPosition(): 傳回選項編號
練習 試設計一個具備「white」、「yellow」、「red」三選項的spinner,點選後可以修改文字標籤的前景顏色
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_height="wrap_content" android:text="選擇網頁" /> <Spinner android:id="@+id/spn" android:drawSelectorOnTop="true"android:prompt="@string/names"
<TextView android:id="@+id/t1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=" " /> <Button android:id="@+id/btn" android:text="GO" </LinearLayout>
package ww.te; import android.R.string; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.TextView;
public class Test2 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); ArrayAdapter<String> adapter_name=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, names); //ArrayAdapter<CharSequence>adapter_name=ArrayAdapter.createFromResource(this, R.array.student_name, android.R.layout.simple_spinner_item); adapter_name.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); spn.setAdapter(adapter_name); setListeners(); }
private TextView t1; private Spinner spn; private Button b1; private String X; private int op; private String names[]={"Google", "Yahoo", "Pchome"}; private void findViews(){ t1=(TextView)findViewById(R.id.t1); spn=(Spinner)findViewById(R.id.spn); b1=(Button)findViewById(R.id.btn); } private void setListeners(){ spn.setOnItemSelectedListener(getName); b1.setOnClickListener(btn);
private Spinner. OnItemSelectedListener getName=new Spinner private Spinner.OnItemSelectedListener getName=new Spinner.OnItemSelectedListener(){ public void onItemSelected(AdapterView<?> adapterView, View v, int position, long id) { op=adapterView.getSelectedItemPosition(); switch(op) { case 0: X="http://www.google.com/webhp?source=search_app"; t1.setText("Google"); break; case 1: X="http://tw.yahoo.com/"; t1.setText("Yahoo"); case 2: X="http://www.pchome.com.tw/"; t1.setText("Pchome"); }
} public void onNothingSelected(AdapterView<?> arg0) { }; private OnClickListener btn=new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Uri uri=Uri.parse(X); Intent myIntent=new Intent(Intent.ACTION_VIEW, uri); startActivity(myIntent);