Android 开发笔记___基本适配器的使用__BaseAdapter

 

之前用到过ArryAdapter适用于纯文本的列表数据,SimpleAdapter适用于带图标的列表数据,但在实际应用中常常有更复杂的列表,比如同一项中存在多个控件,这时候用前面的两个会比较复杂,而且不易扩展。因此Android提供了适应性更强的BaseAdapter,该适配器允许开发者在别的代码文件中进行逻辑处理,大大提高了代码的可读性、可维护性。

 

从BaseAdapter派生的数据适配器主要实现下面三个方法:

  • 构造函数:指定适配器需要处理的数据集合。
  • getCount:获取数据项的个数。
  • getView:获取每项的展示视图,并对每项的内部空间进行业务处理

 

下面以spinner为载体演示如何操作BaseAdapter:

  一、编写列表项的布局文件

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:id="@+id/ll_item"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:orientation="horizontal" >
 6 
 7     <ImageView
 8         android:id="@+id/iv_icon"
 9         android:layout_width="0dp"
10         android:layout_height="80dp"
11         android:layout_weight="1"
12         android:scaleType="fitCenter" />
13 
14     <LinearLayout
15         android:layout_width="0dp"
16         android:layout_height="match_parent"
17         android:layout_weight="3"
18         android:orientation="vertical" >
19 
20         <TextView
21             android:id="@+id/tv_name"
22             android:layout_width="match_parent"
23             android:layout_height="0dp"
24             android:layout_weight="1"
25             android:gravity="left|center"
26             android:textColor="@color/black"
27             android:textSize="20sp" />
28 
29         <TextView
30             android:id="@+id/tv_desc"
31             android:layout_width="match_parent"
32             android:layout_height="0dp"
33             android:layout_weight="2"
34             android:gravity="left|center"
35             android:textColor="@color/black"
36             android:textSize="13sp" />
37     </LinearLayout>
38 
39 </LinearLayout>

  二、写个新的适配器继承BaseAdapter:

 1 package com.example.alimjan.hello_world.adapter;
 2 
 3 import android.content.Context;
 4 import android.view.LayoutInflater;
 5 import android.view.View;
 6 import android.view.ViewGroup;
 7 import android.widget.AdapterView;
 8 import android.widget.AdapterView.OnItemClickListener;
 9 import android.widget.AdapterView.OnItemLongClickListener;
10 import android.widget.BaseAdapter;
11 import android.widget.ImageView;
12 import android.widget.LinearLayout;
13 import android.widget.TextView;
14 import android.widget.Toast;
15 
16 import com.example.alimjan.hello_world.bean.planet;
17 import com.example.alimjan.hello_world.R;
18 
19 import java.util.ArrayList;
20 
21 public class PlanetAdapter extends BaseAdapter implements OnItemClickListener,
22         OnItemLongClickListener {
23 
24     private LayoutInflater mInflater;
25     private Context mContext;
26     private int mLayoutId;
27     private ArrayList<planet> mPlanetList;
28     private int mBackground;
29 
30     public PlanetAdapter(Context context, int layout_id, ArrayList<planet> planet_list, int background) {
31         mInflater = LayoutInflater.from(context);
32         mContext = context;
33         mLayoutId = layout_id;
34         mPlanetList = planet_list;
35         mBackground = background;
36     }
37 
38     @Override
39     public int getCount() {
40         return mPlanetList.size();
41     }
42 
43     @Override
44     public Object getItem(int arg0) {
45         return mPlanetList.get(arg0);
46     }
47 
48     @Override
49     public long getItemId(int arg0) {
50         return arg0;
51     }
52 
53     @Override
54     public View getView(final int position, View convertView, ViewGroup parent) {
55         ViewHolder holder = null;
56         if (convertView == null) {
57             holder = new ViewHolder();
58             convertView = mInflater.inflate(mLayoutId, null);
59             holder.ll_item = (LinearLayout) convertView.findViewById(R.id.ll_item);
60             holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
61             holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
62             holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
63             convertView.setTag(holder);
64         } else {
65             holder = (ViewHolder) convertView.getTag();
66         }
67         planet planet = mPlanetList.get(position);
68         holder.ll_item.setBackgroundColor(mBackground);
69         holder.iv_icon.setImageResource(planet.image);
70         holder.tv_name.setText(planet.name);
71         holder.tv_desc.setText(planet.desc);
72         return convertView;
73     }
74 
75     public final class ViewHolder {
76         private LinearLayout ll_item;
77         public ImageView iv_icon;
78         public TextView tv_name;
79         public TextView tv_desc;
80     }
81 
82     @Override
83     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
84         String desc = String.format("您点击了第%d个行星,它的名字是%s", position + 1,
85                 mPlanetList.get(position).name);
86         Toast.makeText(mContext, desc, Toast.LENGTH_LONG).show();
87     }
88 
89     @Override
90     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
91         String desc = String.format("您长按了第%d个行星,它的名字是%s", position + 1,
92                 mPlanetList.get(position).name);
93         Toast.makeText(mContext, desc, Toast.LENGTH_LONG).show();
94         return true;
95     }
96 }

  三、在页面代码中构造该适配器,,并应用于spinner对象:

 1 package com.example.alimjan.hello_world;
 2 
 3 import java.util.ArrayList;
 4 
 5 /**
 6  * Created by alimjan on 7/16/2017.
 7  */
 8 
 9         import com.example.alimjan.hello_world.adapter.PlanetAdapter;
10         import com.example.alimjan.hello_world.bean.planet;
11         import android.content.Context;
12         import android.content.Intent;
13         import android.graphics.Color;
14         import android.os.Bundle;
15         import android.support.v7.app.AppCompatActivity;
16         import android.view.View;
17         import android.widget.AdapterView;
18         import android.widget.Spinner;
19         import android.widget.Toast;
20         import android.widget.AdapterView.OnItemSelectedListener;
21 
22 public class class_5_2_1 extends AppCompatActivity {
23 
24     private ArrayList<planet> planetList;
25 
26     @Override
27     protected void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.code_5_2_1);
30         initSpinner();
31     }
32 
33     private void initSpinner() {
34         planetList = planet.getDefaultList();
35         PlanetAdapter adapter = new PlanetAdapter(this, R.layout.item_list, planetList, Color.WHITE);
36         Spinner sp = (Spinner) findViewById(R.id.sp_planet);
37         sp.setPrompt("请选择行星");
38         sp.setAdapter(adapter);
39         sp.setSelection(0);
40         sp.setOnItemSelectedListener(new MySelectedListener());
41     }
42 
43     private class MySelectedListener implements OnItemSelectedListener {
44         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
45             Toast.makeText(class_5_2_1.this, "您选择的是"+planetList.get(arg2).name, Toast.LENGTH_LONG).show();
46         }
47 
48         public void onNothingSelected(AdapterView<?> arg0) {
49         }
50     }
51 
52     public static void startHome(Context mContext) {
53         Intent intent = new Intent(mContext, class_5_2_1.class);
54         mContext.startActivity(intent);
55     }
56 
57 }

plenet 类在这

 1 package com.example.alimjan.hello_world.bean;
 2 
 3 import java.util.ArrayList;
 4 import com.example.alimjan.hello_world.R;
 5 
 6 /**
 7  * Created by alimjan on 7/16/2017.
 8  */
 9 
10 public class planet {
11     public int image;
12     public String name;
13     public String desc;
14 
15     public planet() {
16         this.image = 0;
17         this.name = "";
18         this.desc = "";
19     }
20 
21     public planet(int image, String name, String desc) {
22         this.image = image;
23         this.name = name;
24         this.desc = desc;
25     }
26 
27     private static int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
28             R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
29     private static String[] nameArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
30     private static String[] descArray = {
31             "水星是太阳系八大行星最内侧也是最小的一颗行星,也是离太阳最近的行星",
32             "金星是太阳系八大行星之一,排行第二,距离太阳0.725天文单位",
33             "地球是太阳系八大行星之一,排行第三,也是太阳系中直径、质量和密度最大的类地行星,距离太阳1.5亿公里",
34             "火星是太阳系八大行星之一,排行第四,属于类地行星,直径约为地球的53%",
35             "木星是太阳系八大行星中体积最大、自转最快的行星,排行第五。它的质量为太阳的千分之一,但为太阳系中其它七大行星质量总和的2.5倍",
36             "土星为太阳系八大行星之一,排行第六,体积仅次于木星"
37     };
38     public static ArrayList<planet> getDefaultList() {
39         ArrayList<planet> planetList = new ArrayList<planet>();
40         for (int i=0; i<iconArray.length; i++) {
41             planetList.add(new planet(iconArray[i], nameArray[i], descArray[i]));
42         }
43         return planetList;
44     }
45 }
View Code

 

posted @ 2017-07-18 10:15  alm  阅读(236)  评论(0编辑  收藏  举报