【Android】Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法
发布日期:2021-06-29 20:54:47 浏览次数:2 分类:技术文章

本文共 3723 字,大约阅读时间需要 12 分钟。

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

原文来源:http://blog.csdn.net/shakespeare001/article/details/7926783

1.ArrayAdapter

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

代码片段:

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


2.SimpleAdapter

可以显示比较复杂的列表,包括每行显示图片、文字等,但不能对列表进行后期加工(在java代码中加工),

也是只是单纯的负责显示(当然可以设计复杂点的布局来显示复杂列表),例如,每行显示不同背景等。

代码片段:

[java] 
  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] 
  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.         }  


转载地址:https://dengjin.blog.csdn.net/article/details/41577083 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【Android】Intent介绍及Intent在Activity中的使用方法
下一篇:【Java】eclipse如何设置成保护眼的背景色

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年05月03日 01时26分36秒