Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段

(转自:http://blog.csdn.net/shakespeare001/article/details/7926783


收藏ArrayAdapter、SimpleAdapter和BaseAdapter的一些简短代码片段,希望用时方便想起其用法。

1.ArrayAdapter


只可以简单的显示一行文本

代码片段:

[java]  view plain copy
  1. ArrayAdapter<String> adapter = new ArrayAdapter<String>(  
  2.                 this,  
  3.                 R.layout.item,//只能有一个定义了id的TextView  
  4.                 data);//data既可以是数组,也可以是List集合  


2.SimpleAdapter


可以显示比较复杂的列表,包括每行显示图片、文字等,但不能对列表进行后期加工(在java代码中加工),
也是只是单纯的负责显示(当然可以设计复杂点的布局来显示复杂列表),例如,每行显示不同背景等。

代码片段:

[java]  view plain copy
  1. List<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();;  
  2. for(int i =0; i < lengh; i++) {      
  3.     Map<String,Object> item = new HashMap<String,Object>();      
  4.     item.put("image", R.drawable.portrait);      
  5.     item.put("title", mListTitle[i]);      
  6.     item.put("text", mListStr[i]);      
  7.     mData.add(item);       
  8. }      
  9. SimpleAdapter adapter = new SimpleAdapter(  
  10.                 this,  
  11.                 mData,  
  12.                 R.layout.item,      
  13.                 new String[]{"image","title","text"},  
  14.                 new int[]{R.id.image,R.id.title,R.id.text});     


3.BaseAdapter


可以实现复杂的列表布局,由于BaseAdapter是一个抽象类,使用该类需要自己写一个适配器继承该类,
正是由于继承了该类,需要我们重写一些方法,让我们可以在代码里控制列表的样式,更加灵活。

代码片段:

[java]  view plain copy
  1. private class MyListAdapter extends BaseAdapter{    
  2.         private Context mContext;    
  3.         private int[] colors=new int[]{0xff626569,0xff4f5257 };    
  4.          public MyListAdapter(Context context){    
  5.              mContext=context;    
  6.          }    
  7.         @Override    
  8.         public int getCount() {    
  9.             // TODO Auto-generated method stub    
  10.             return mListText.length;    
  11.         }    
  12.     
  13.         @Override    
  14.         public Object getItem(int position) {    
  15.             // TODO Auto-generated method stub    
  16.             return position;    
  17.         }    
  18.     
  19.         @Override    
  20.         public long getItemId(int position) {    
  21.             // TODO Auto-generated method stub    
  22.             return position;    
  23.         }    
  24.     
  25.         @Override    
  26.         public View getView(int position, View convertView, ViewGroup parent) {    
  27.             ImageView image=null;  //这些控件可以单独封装成一个类(Holder),便与优化  
  28.             TextView title=null;    
  29.             TextView  content=null;    
  30.             if(convertView==null){    
  31.                 convertView=LayoutInflater.from(mContext).inflate(R.layout.colorlist, null);    
  32.                 image=(ImageView) convertView.findViewById(R.id.color_image);    
  33.                 title=(TextView) convertView.findViewById(R.id.color_title);    
  34.                 content=(TextView) convertView.findViewById(R.id.color_text);    
  35.             }   
  36.             int colorPos=position%colors.length;    
  37.             convertView.setBackgroundColor(colors[colorPos]);    
  38.             title.setText(mListTitle[position]);    
  39.             content.setText(mListText[position]);    
  40.             image.setImageResource(R.drawable.portrait);    
  41.                 
  42.             return convertView;    
  43.         }    
  44.             
  45.     }    
  46.   
  47. --------------------------下面样例列表页的控件单独封装成了一个类(Holder),便与优化-----  
  48.   
  49.   
  50.  public class MyBaseAdapter extends BaseAdapter{  
  51.        
  52.            private LayoutInflater mInflater;  
  53.             public MyAdapter(Context context){  
  54.                this.mInflater = LayoutInflater.from(context);  
  55.           }  
  56.             @Override  
  57.            public int getCount() {  
  58.                 // TODO Auto-generated method stub  
  59.                 return mData.size();  
  60.             }  
  61.        
  62.             @Override  
  63.             public Object getItem(int arg0) {  
  64.                 // TODO Auto-generated method stub  
  65.                 return null;  
  66.            }  
  67.        
  68.             @Override  
  69.             public long getItemId(int arg0) {  
  70.                 // TODO Auto-generated method stub  
  71.                 return 0;  
  72.             }  
  73.        
  74.             @Override  
  75.             public View getView(int position, View convertView, ViewGroup parent) {  
  76.                    
  77.                 ViewHolder holder = null;  
  78.                 if (convertView == null) {  
  79.                     holder=new ViewHolder();   
  80.                     convertView = mInflater.inflate(R.layout.vlist2, null);  
  81.                     holder.img = (ImageView)convertView.findViewById(R.id.img);  
  82.                     holder.title = (TextView)convertView.findViewById(R.id.title);  
  83.                     holder.info = (TextView)convertView.findViewById(R.id.info);  
  84.                     holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);  
  85.                     convertView.setTag(holder);  
  86.                        
  87.                 }else {                       
  88.                     holder = (ViewHolder)convertView.getTag();  
  89.                 }  
  90.                 holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));  
  91.                 holder.title.setText((String)mData.get(position).get("title"));  
  92.                 holder.info.setText((String)mData.get(position).get("info"));  
  93.                 //给每一个列表后面的按钮添加响应事件  
  94.                 holder.viewBtn.setOnClickListener(new View.OnClickListener() {  
  95.                     @Override  
  96.                     public void onClick(View v) {  
  97.                         showInfo();                  
  98.                     }  
  99.                 });  
  100.   
  101.                 return convertView;  
  102.             }  
  103.         ------------  
  104.         public final class ViewHolder{  
  105.             public ImageView img;  
  106.             public TextView title;  
  107.             public TextView info;  
  108.             public Button viewBtn;  
  109.         }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值