spinner和適配器模式

來源:互聯網
上載者:User

標籤:android   io   os   ar   資料   sp   cti   log   on   

spinner相當於html表單中的select下拉式清單。

------------
|第一種方式|
------------
在string.xml中添加一個數組spinner_data:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="spinner_data">
<item >北京</item>
<item >湖南</item>
<item >湖北</item>
</string-array>
</resources>

拖拽一個spinner:
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/spinner_data"
android:spinnerMode="dialog"

/>

<!-- entries 顯示spinner當中的資料項目
spinnerMode="dropdown"是預設值下拉式清單,spinnerMode="dialog"是以對話方塊的形式顯示--!>
------------
|第二種方式|
------------
BaseAdapter就Android應用程式中經常用到的基礎資料配接器,它的主要用途是將一組資料傳到像ListView、Spinner、Gallery、GridView等UI顯示組件,它是自動繼承介面類Adapter。
假如要往spinner中添加string[]或List。
--------------------------------------------------------------------------------------------------------------
a)string[]
在MainActivity主類中定義一個字串數組:
private String[] str = new String[] {
  "山東","山西","北京"
};


定義一個繼承BaseAdapter的類:
private class MyAdapter extends BaseAdapter {

@Override
public int getCount() {
  // TODO Auto-generated method stub
  return str.length;//重要方法
}

@Override
public Object getItem(int arg0) {
  // TODO Auto-generated method stub
  return null;
}

@Override
public long getItemId(int arg0) {
  // TODO Auto-generated method stub
  return 0;
}

@Override
public View getView(int position, View view, ViewGroup group) {//重要方法
  // TODO Auto-generated method stub
  //str.length多長此方法就執行幾次
  TextView textView = new TextView(MainActivity.this);
  textView.setText(str[position]);
  return textView;
}

}


在onCreate方法中:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(new MyAdapter());
--------------------------------------------------------------------------------------------------------------
b)List
在MainActivity主類中定義一個List:
private List list = new ArrayList();

並在onCreate方法中新增內容:
list.add("上海");
list.add("天津");
list.add("浙江");

 

MyAdapter類則只需要修改幾處即可:
1.public int getCount() {

  return list.size();
}

2.public View getView(int position, View view, ViewGroup group) {

  TextView textView = new TextView(MainActivity.this);
  textView.setText((CharSequence) list.get(position));
  return textView;
}

3.還可以繼續給spinner添加事件OnItemSelectedListener:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub

  Toast.makeText(MainActivity.this, (CharSequence) list.get(position), 0).show();


}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub



}


});

--------------------------------------------------------------------------------------------------------------

 

spinner和適配器模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.