Android之BaseAdapter适配器使用技巧

今天给大家带来的是BaseAdapter适配器的使用技巧。想来有过Android开发经验的开发者都知道Android适配器吧。没错,Android适配器就是将一些数据恰当的在view中显示,说白了就是数据和视图之间的桥梁。Android适配器有ArrayAdapter、SimpleAdapter、BaseAdapter等等。现在许多Android应用都要用到适配器,而BaseAdapter适配器相比其他的适配器更受开发者的喜爱。因为它可以按照开发者的自己的想法来定制自己的适配器,而不是固定死。当然BaseAdapter适配器也可以完成像ArrayAdapter、SimpleAdapter适配器一样的功能。本章主要是讲BaseAdapter适配器的使用方法。

ok!一起来写一写一个简单的BaseAdapter的应用吧!新建名为BaseAdapterDemo的Android工程项目,目录如下:

先来看看主布局文件activity_main.xml:

[html] view plain copy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <ListView   
  12.         android:id="@+id/listview"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         />  
  16.   
  17. </RelativeLayout>  

没错,就是那么简单。整个界面就一个ListView,用它来显示数据。

再来看看ListView显示的数据的布局文件list_item_layout.xml:

[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.       
  7.     <ImageView   
  8.         android:id="@+id/imageview"  
  9.         android:layout_width="50dip"  
  10.         android:layout_height="50dip"  
  11.         />  
  12.       
  13.     <TextView   
  14.         android:id="@+id/textview"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="match_parent"  
  17.         android:paddingTop="8dip"  
  18.         android:textSize="20sp"  
  19.         />  
  20.       
  21.     <Button   
  22.         android:id="@+id/button"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         />  
  26. </LinearLayout>  

该布局文件也很简单,就一个ImageView,一个TextView和一个Button组成。用于ListView每一行的布局。
 

接下来就是整个应用的关键了,MyAdapter.java:

[java] view plain copy

  1. package com.example.adapter;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import com.example.baseadapterdemo.R;  
  7.   
  8. import android.content.Context;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.widget.BaseAdapter;  
  13. import android.widget.Button;  
  14. import android.widget.ImageView;  
  15. import android.widget.TextView;  
  16.   
  17. /** 
  18.  * MyAdapter继承BaseAdapter 
  19.  *  
  20.  * @author Joker_Ya 
  21.  *  
  22.  */  
  23. public class MyAdapter extends BaseAdapter {  
  24.   
  25.     private List<Map<String, Object>> datas;  
  26.     private Context mContext;  
  27.   
  28.     /** 
  29.      * 构造函数 
  30.      *  
  31.      * @param datas 
  32.      *            需要绑定到view的数据 
  33.      * @param mContext 
  34.      *            传入上下文 
  35.      */  
  36.     public MyAdapter(List<Map<String, Object>> datas, Context mContext) {  
  37.         this.datas = datas;  
  38.         this.mContext = mContext;  
  39.     }  
  40.   
  41.     @Override  
  42.     public int getCount() {  
  43.         // TODO Auto-generated method stub  
  44.         return datas.size();// 返回数据的总数  
  45.     }  
  46.   
  47.     @Override  
  48.     public Object getItem(int position) {  
  49.         // TODO Auto-generated method stub  
  50.         return datas.get(position);// 返回在list中指定位置的数据的内容  
  51.     }  
  52.   
  53.     @Override  
  54.     public long getItemId(int position) {  
  55.         // TODO Auto-generated method stub  
  56.         return position;// 返回数据在list中所在的位置  
  57.     }  
  58.   
  59.     @Override  
  60.     public View getView(int position, View convertView, ViewGroup parent) {  
  61.         // TODO Auto-generated method stub  
  62.         final ViewHolder holder;  
  63.         if (convertView == null) {  
  64.             // 使用自定义的list_items作为Layout  
  65.             convertView = LayoutInflater.from(mContext).inflate(  
  66.                     R.layout.list_item_layout, null);  
  67.             // 减少findView的次数  
  68.             holder = new ViewHolder();  
  69.             // 初始化布局中的元素  
  70.             holder.mImageView = (ImageView) convertView  
  71.                     .findViewById(R.id.imageview);  
  72.             holder.mTextView = (TextView) convertView  
  73.                     .findViewById(R.id.textview);  
  74.             holder.mButton = (Button) convertView.findViewById(R.id.button);  
  75.   
  76.             convertView.setTag(holder);  
  77.         } else {  
  78.             holder = (ViewHolder) convertView.getTag();  
  79.         }  
  80.         // 从传入的数据中提取数据并绑定到指定的view中  
  81.         holder.mImageView.setImageResource((Integer) datas.get(position).get(  
  82.                 "img"));  
  83.         holder.mTextView.setText(datas.get(position).get("title").toString());  
  84.         holder.mButton.setText(datas.get(position).get("button").toString());  
  85.   
  86.         return convertView;  
  87.     }  
  88.   
  89.     static class ViewHolder {  
  90.         ImageView mImageView;  
  91.         TextView mTextView;  
  92.         Button mButton;  
  93.     }  
  94. }  

MyAdapter类继承BaseAdapter,然后在getView()方法中设置ListView每一行的布局和数据绑定。其实也很简单,首先调用LayoutInflater的inflate()方法设置每一行的布局,然后得到布局中的各个元素,并把传入的数据设置在各个元素上即可。这样一来就完成自定义的Adapter。

最后是MainActivity.java

[java] view plain copy

  1. package com.example.baseadapterdemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.widget.ListView;  
  11.   
  12. import com.example.adapter.MyAdapter;  
  13.   
  14. /** 
  15.  * BaseAdapter使用技巧 
  16.  *  
  17.  * @author Joker_Ya 
  18.  *  
  19.  */  
  20. public class MainActivity extends Activity {  
  21.   
  22.     private ListView mListView;  
  23.     private MyAdapter myAdapter;  
  24.   
  25.     private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
  26.   
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_main);  
  31.         initData();// 初始化数据  
  32.         mListView = (ListView) findViewById(R.id.listview);  
  33.         myAdapter = new MyAdapter(list, this);  
  34.         // 设置mListView的适配器  
  35.         mListView.setAdapter(myAdapter);  
  36.     }  
  37.   
  38.     /** 
  39.      * 模拟从网络上得到的数据 
  40.      */  
  41.     private void initData() {  
  42.         // TODO Auto-generated method stub  
  43.         Map<String, Object> map = new HashMap<String, Object>();  
  44.   
  45.         map.put("img", R.drawable.su01);  
  46.         map.put("title", "数字零");  
  47.         map.put("button", "OPEN");  
  48.         list.add(map);  
  49.   
  50.         map = new HashMap<String, Object>();  
  51.         map.put("img", R.drawable.su02);  
  52.         map.put("title", "数字一");  
  53.         map.put("button", "OPEN");  
  54.         list.add(map);  
  55.   
  56.         map = new HashMap<String, Object>();  
  57.         map.put("img", R.drawable.su03);  
  58.         map.put("title", "数字二");  
  59.         map.put("button", "OPEN");  
  60.         list.add(map);  
  61.   
  62.         map = new HashMap<String, Object>();  
  63.         map.put("img", R.drawable.su04);  
  64.         map.put("title", "数字三");  
  65.         map.put("button", "OPEN");  
  66.         list.add(map);  
  67.   
  68.         map = new HashMap<String, Object>();  
  69.         map.put("img", R.drawable.su05);  
  70.         map.put("title", "数字四");  
  71.         map.put("button", "OPEN");  
  72.         list.add(map);  
  73.   
  74.         map = new HashMap<String, Object>();  
  75.         map.put("img", R.drawable.su06);  
  76.         map.put("title", "数字五");  
  77.         map.put("button", "OPEN");  
  78.         list.add(map);  
  79.   
  80.         map = new HashMap<String, Object>();  
  81.         map.put("img", R.drawable.su07);  
  82.         map.put("title", "数字六");  
  83.         map.put("button", "OPEN");  
  84.         list.add(map);  
  85.   
  86.         map = new HashMap<String, Object>();  
  87.         map.put("img", R.drawable.su08);  
  88.         map.put("title", "数字七");  
  89.         map.put("button", "OPEN");  
  90.         list.add(map);  
  91.   
  92.         map = new HashMap<String, Object>();  
  93.         map.put("img", R.drawable.su09);  
  94.         map.put("title", "数字八");  
  95.         map.put("button", "OPEN");  
  96.         list.add(map);  
  97.   
  98.         map = new HashMap<String, Object>();  
  99.         map.put("img", R.drawable.su10);  
  100.         map.put("title", "数字九");  
  101.         map.put("button", "OPEN");  
  102.         list.add(map);  
  103.     }  
  104.   
  105. }  

好了,主Activity内容很简单,就不多说了。这样一来整个应用就完成了,接下来就看看结果吧:

结果没有问题,和想象的一样。当然那个Button还是别点了,点了也没有用。因为我们没有写点击事件啊!大笑

最后的最后附上源码下载地址:

源码下载

猜你喜欢

转载自blog.csdn.net/hljqfl/article/details/85857340