Spinner 绑定 Arraylist .
来源:互联网 发布:flv视频编辑软件 编辑:程序博客网 时间:2024/06/13 14:15
Spinner绑定Arraylist,其实跟ListView绑定Arraylist差不多.
public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create and display a Spinner: Spinner s = new Spinner(this); AbsListView.LayoutParams params = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT ); this.setContentView(s, params); // fill the ArrayList: List<Guy> guys = new ArrayList<Guy>(); guys.add(new Guy("Lukas", 18)); guys.add(new Guy("Steve", 20)); guys.add(new Guy("Forest", 50)); MyAdapter adapter = new MyAdapter(guys); // apply the Adapter: s.setAdapter(adapter); // onClickListener: s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { /** * Called when a new item was selected (in the Spinner) */ public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Guy g = (Guy) parent.getItemAtPosition(pos); Toast.makeText( getApplicationContext(), g.getName()+" is "+g.getAge()+" years old.", Toast.LENGTH_LONG ).show(); } public void onNothingSelected(AdapterView parent) { // Do nothing. } }); } /** * This is your own Adapter implementation which displays * the ArrayList of "Guy"-Objects. */ private class MyAdapter extends BaseAdapter implements SpinnerAdapter { /** * The internal data (the ArrayList with the Objects). */ private final List<Guy> data; public MyAdapter(List<Guy> data){ this.data = data; } /** * Returns the Size of the ArrayList */ @Override public int getCount() { return data.size(); } /** * Returns one Element of the ArrayList * at the specified position. */ @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int i) { return i; } /** * Returns the View that is shown when a element was * selected. */ @Override public View getView(int position, View recycle, ViewGroup parent) { TextView text; if (recycle != null){ // Re-use the recycled view here! text = (TextView) recycle; } else { // No recycled view, inflate the "original" from the platform: text = (TextView) getLayoutInflater().inflate( android.R.layout.simple_dropdown_item_1line, parent, false ); } text.setTextColor(Color.BLACK); text.setText(data.get(position).name); return text; } } /** * A simple class which holds some information-fields * about some Guys. */ private class Guy{ private final String name; private final int age; public Guy(String name, int age){ this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }}
摘自:<a target=_blank href="http://stackoverflow.com/questions/6562236/android-spinner-databind-using-array-list?rq=1">http://stackoverflow.com/questions/6562236/android-spinner-databind-using-array-list?rq=1</a>
0 0
- Spinner 绑定 Arraylist .
- activity中实现Spinner绑定
- Spinner绑定键值对信息
- Android Spinner绑定默认项
- arraylist对象数组绑定
- ArrayList绑定GridView
- android spinner 两种列表 和 ArrayList
- Android 之 Spinner 键值对的绑定
- Android 之 Spinner 键值对的绑定
- Android 之 Spinner 键值对的绑定
- Android 之 Spinner 键值对的绑定
- 关于读取数据库绑定Spinner显示问题
- Android控件Spinner监听之绑定ImageView
- Android 之 Spinner 键值对的绑定
- ComboBox绑定IList 或 ArrayList
- 如何获取Spinner中绑定的所有数据 or 如何直接向Spinner绑定字符串
- C# Repeater绑定数组,ArrayList和ArrayList实体类
- Spinner
- Linux加载DTS设备节点的过程(以高通8974平台为例)
- hibernate映射
- 阿里CEO陆兆禧:注册双十一是为保护这个节日
- QuickXDev增强功能:Run With Player
- DotNetBar.Bar图标列表的使用
- Spinner 绑定 Arraylist .
- oracle数据泵impdp命令导入报错UDI-00013: Message 13 not found; No message file for product=RDBMS, facility=UDI
- JAVA MessageDigest(MD5加密等)
- java date类型和calendar类型区别
- 吐槽
- LinkedList,ArrayList末尾插入谁效率高?
- google地图网络服务API
- 读取一串整数,寻找合适的数据结构和算法,实现插入元素track(int x) 方法,以及GetRankOfNumber(int x)方法,返回值为小于等于x的元素个数(不包括x本身)
- CocoStudio UI编辑器自定义插件