自定义ListView中的item

来源:互联网 发布:淘宝网店送什么小赠品 编辑:程序博客网 时间:2024/06/17 21:28

[功能]
* AdapterView
- ListView
- GridView
- Gallery
- Spinner

* Adapter
- SimpleAdapter
- SimpleCursorAdapter
- ArrayAdapter

至于 AdapterView & Adapter 如何选择的问题 有2点需要注意:
× AdapterView  的选择 只和界面有关 和具体数据无关
× Adapter 的选择 只喝数据有关 和界面无关
二者耦合度高 互不干涉!

android给出的AdapterView中所使用的Adapter的item都是TextView 即 只能显示一下文字信息 这就限制了它的应用 所以现在告诉大家怎么使用别的View


[思路]
1. 自定义一个 extends BaseAdapter 的 class 如 public class CustomList extends BaseAdapter
2. 填充 CustomList 的一些方法 如下:

Java代码 复制代码
  1. public int getCount()   
  2. public Object getItem(int position)   
  3. public long getItemId(int position)   
  4. public View getView(int position, View convertView, ViewGroup parent)  




[代码]
1. 比如 现在有下列数据 要求显示之

Java代码 复制代码
  1. String[] week = {   
  2.     "JAN","FEB","MAR","APR",   
  3.             "MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC "  
  4.     };  




2. 一些函数的定义如下

Java代码 复制代码
  1. public class CustomList extends BaseAdapter {   
  2.         Activity activity;   
  3.            
  4.         //construct   
  5.         public CustomList(Activity a ) {   
  6.             activity = a;   
  7.         }   
  8.            
  9.         @Override  
  10.         public int getCount() {   
  11.             // TODO Auto-generated method stub   
  12.             return week.length;   
  13.         }   
  14.   
  15.         @Override  
  16.         public Object getItem(int position) {   
  17.             // TODO Auto-generated method stub   
  18.             return week[position];   
  19.         }   
  20.   
  21.         @Override  
  22.         public long getItemId(int position) {   
  23.             // TODO Auto-generated method stub   
  24.             return position;   
  25.         }   
  26.   
  27.         @Override  
  28.         public View getView(int position, View convertView, ViewGroup parent) {   
  29.             // TODO Auto-generated method stub   
  30.             TextView tv = new TextView(activity);   
  31.             tv.setText(week[position]);   
  32.             return tv;   
  33.         }   
  34.            
  35.     }  



3. 考虑到美观 我们可以把getView()的一些填充提取出来 即 根据目标的position 得到目标所需View

Java代码 复制代码
  1. public View addCustomView(int position){   
  2.             View view = new View(activity);   
  3.                
  4.             switch(position){   
  5.             case 11:   
  6.                 Button btn = new Button(activity);   
  7.                 btn.setText("Yes!");   
  8.                    
  9.                 view = btn;   
  10.             case 12:   
  11.                 ImageView iv = new ImageView(activity);   
  12.                 iv.setImageResource(R.drawable.robot);   
  13.                    
  14.                 view = iv;   
  15.                    
  16.                 break;   
  17.             default:   
  18.                 TextView tv = new TextView(activity);   
  19.                 tv.setGravity(1);   
  20.                 tv.setText(week[position]);   
  21.                 tv.setPadding(5555);   
  22.                 view = tv;   
  23.             }   
  24.                
  25.             return view;   
  26.         }  



4. 如何使用

Java代码 复制代码
  1. public View getView(int position, View convertView, ViewGroup parent) {   
  2.             // TODO Auto-generated method stub   
  3.             return addCustomView(position);   
  4.         }  




这样 我们如果要定制某个position的View 就可以增加相应的case..


done!

 

转载来自:http://www.javaeye.com/topic/568927

原创粉丝点击