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,點選後可以修改文字標籤的前景顏色
package yoo.yoo; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class Yoo extends Activity { /** Called when the activity is first created. */ private Spinner sp; private TextView text; int i; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text=(TextView)findViewById(R.id.txt); sp=(Spinner)findViewById(R.id.sp); String color[]={"yellow", "red", "white"}; ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, color); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(adapter); sp.setOnItemSelectedListener(getName); } private Spinner.OnItemSelectedListener getName=new Spinner.OnItemSelectedListener(){ public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub i=arg0.getSelectedItemPosition(); if(i==0)text.setTextColor(Color.YELLOW); if(i==1)text.setTextColor(Color.RED); if(i==2)text.setTextColor(Color.WHITE); public void onNothingSelected(AdapterView<?> arg0) { }; ================================網頁================================ package two.two; import android.content.Intent; import android.net.Uri; public class two extends Activity { @Override findViews(); adapter(); setListeners(); } private String names[]={"Yahoo", "Google", "Pchome"}; private Spinner sp; private void findViews(){ sp=(Spinner)findViewById(R.id.sp); private void adapter(){ ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, names); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(adapter); private void setListeners(){ int a; a=arg0.getSelectedItemPosition(); if(a==0){ Uri u=Uri.parse("http://yahoo.com.tw"); Intent i =new Intent(Intent.ACTION_VIEW,u); startActivity(i); if(a==1){ Uri u1=Uri.parse("http://google.com.tw"); Intent i1 =new Intent(Intent.ACTION_VIEW,u1); startActivity(i1); if(a==2){ Uri u2=Uri.parse("http://pchome.com.tw"); Intent i2 =new Intent(Intent.ACTION_VIEW,u2); startActivity(i2);