由于在使用过程中如果只使用系统自带的适配器有很多都不能满足要求,所以自定义适配器是非常有必要的。我们自定义的适配器都是要定义一个类继承自BaseAdapter,实现里面的如下方法:
<span style="font-size:18px;">public class MyAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return 0; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub return null; } }</span>
此处以ListView的格式来做一个自定义的适配器:
先定义一个Bean以便发送和接收消息,此处我定义的为InformationBean.java其代码如下:
<span style="font-size:18px;">package com.sc.android.activity.bean; public class InformationBean { private String title; private String msg; private int icon; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getIcon() { return icon; } public void setIcon(int icon) { this.icon = icon; } } </span>
自定义的适配器MyAdapter代码如下:
<span style="font-size:18px;">package com.sc.android.ui.listview; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.sc.android.R; import com.sc.android.activity.bean.InformationBean; import com.sc.android.util.Logs; public class MyAdapter extends BaseAdapter { private Context mContext; private List<InformationBean> mList = new ArrayList<InformationBean>();; private LayoutInflater mInflater; private ContentMsg contentMsg; public MyAdapter(Context context, List<InformationBean> list) { mContext = context; mInflater = LayoutInflater.from(context); } /** * 设置数据源,刷新适配器 */ public void setData(List<InformationBean> list) { mList = list; notifyDataSetChanged();// 通知刷新适配器数据 } /** * 返回容器中元素个数 */ @Override public int getCount() { return mList.size(); } /** * 返回容器中指定位置的数据项 */ @Override public Object getItem(int position) { return mList.get(position); } /** * 返回容器中指定位置的ID */ @Override public long getItemId(int position) { return position; } /** * 返回表示行的view */ @Override public View getView(int position, View convertView, ViewGroup parent) { contentMsg = null; if (convertView == null) { // 将第一个参数指定的布局文件转换成View对象,如果第二参数ViewGroup不为空,则把view对象添加到该ViewGroup中 convertView = mInflater.inflate( R.layout.activity_pos_listview_layout, null); contentMsg = new ContentMsg(); contentMsg.iconImg = (ImageView) convertView .findViewById(R.id.pic_img); contentMsg.textTitle = (TextView) convertView .findViewById(R.id.title_txt); contentMsg.textMsg = (TextView) convertView .findViewById(R.id.msg_txt); convertView.setTag(contentMsg); Logs.e("创建新的View对象" + convertView); } else { contentMsg = (ContentMsg) convertView.getTag(); Logs.e("重复以前的View对象" + convertView); } InformationBean information = (InformationBean) getItem(position); // 更新每一项中数据 contentMsg.iconImg.setImageResource(information.getIcon()); contentMsg.textTitle.setText(information.getTitle()); contentMsg.textMsg.setText(information.getMsg()); return convertView; } class ContentMsg { ImageView iconImg; TextView textTitle; TextView textMsg; } } </span>
所以建立了一个Activity来显示,MyAdapterViewActivity代码如下:
<span style="font-size:18px;">package com.sc.android.ui.listview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import com.sc.android.R; import com.sc.android.activity.bean.InformationBean; public class MyAdapterViewActivity extends Activity { private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview_layout); mListView = (ListView) findViewById(R.id.listsview); MyAdapter adapter = new MyAdapter(this, getData()); mListView.setAdapter(adapter); } public List<InformationBean> getData() { List<InformationBean> list = new ArrayList<InformationBean>(); InformationBean information = new InformationBean(); information.setTitle("重庆火巴耳朵火锅"); information.setIcon(R.drawable.ped); information.setMsg("[音乐学院] 庆开业代金券,全场通用"); list.add(information); information = new InformationBean(); information.setTitle("小肥羊"); information.setIcon(R.drawable.xfy); information.setMsg("[13店通用] 代金券,可叠加,不限"); list.add(information); information = new InformationBean(); information.setTitle("泸小胖龙虾"); information.setIcon(R.drawable.xplx); information.setMsg("[淮海路] 现金券,全场通用,可叠加"); list.add(information); information = new InformationBean(); information.setTitle("重庆火巴耳朵火锅"); information.setIcon(R.drawable.ped); information.setMsg("[音乐学院] 庆开业代金券,全场通用"); list.add(information); information = new InformationBean(); information.setTitle("小肥羊"); information.setIcon(R.drawable.xfy); information.setMsg("[13店通用] 代金券,可叠加,不限"); list.add(information); information = new InformationBean(); information.setTitle("泸小胖龙虾"); information.setIcon(R.drawable.xplx); information.setMsg("[淮海路] 现金券,全场通用,可叠加"); list.add(information); information = new InformationBean(); information.setTitle("重庆火巴耳朵火锅"); information.setIcon(R.drawable.ped); information.setMsg("[音乐学院] 庆开业代金券,全场通用"); list.add(information); information = new InformationBean(); information.setTitle("小肥羊"); information.setIcon(R.drawable.xfy); information.setMsg("[13店通用] 代金券,可叠加,不限"); list.add(information); information = new InformationBean(); information.setTitle("泸小胖龙虾"); information.setIcon(R.drawable.xplx); information.setMsg("[淮海路] 现金券,全场通用,可叠加"); list.add(information); information = new InformationBean(); information.setTitle("重庆火巴耳朵火锅"); information.setIcon(R.drawable.ped); information.setMsg("[音乐学院] 庆开业代金券,全场通用"); list.add(information); information = new InformationBean(); information.setTitle("小肥羊"); information.setIcon(R.drawable.xfy); information.setMsg("[13店通用] 代金券,可叠加,不限"); list.add(information); information = new InformationBean(); information.setTitle("泸小胖龙虾"); information.setIcon(R.drawable.xplx); information.setMsg("[淮海路] 现金券,全场通用,可叠加"); list.add(information); return list; } } </span>
以上Java文件的xml代码如下:
activity_pos_listview_layout.xml:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/simple_view" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/pic_img" android:layout_width="85dp" android:layout_height="85dp" android:layout_marginLeft="5dp" android:src="@drawable/ped" /> <TextView android:id="@+id/title_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="13dp" android:layout_toRightOf="@id/pic_img" android:text="重庆火巴耳朵火锅" android:textSize="16dp" /> <TextView android:id="@+id/msg_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/title_txt" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_toRightOf="@id/pic_img" android:text="[音乐学院] 庆开业代金券,全场通用" /> </RelativeLayout></span>
activity_listview_layout.xml:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listsview" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout></span>
其中图片资源放在drawable文件夹下面。