数据适配器Adapter
- 格式:ppt
- 大小:962.00 KB
- 文档页数:26


Android Adapter深刻详解我在刚玩android 时候,对这个adapter很不理解,到底是什么原理呢?适配器,哎,只知道setAdapter()把参数传进去,系统就显示出来了。
今天,针对这个东西,我们做个系统详细的分析.listview加载adapter过程是这样的.1先判断adapter 有多少数据项,根据这个数据确定有多少item.2确定每个item里加载哪个View.3把View里加载要显示的数据.问提一个一个来解决.第一个问题:因为adapter都要关联一个list .有来存储数据.list的项数就是Item的数目.我们在重载BaseAdapter时候,都要实现这个函数public intgetCount() {return weatherList.size();}哎,这个函数就是确定关联条目的.第二个问题哪来的view 呢,当然我们自己创建的.重载BaseAdapter时候你要实现getView()这个函数,就是这个view.第三个问题,你自己创建的view.加载哪些数据你该知道的.呵呵.张豪就喜欢看例子,这个小伙子技术,管理都很牛,得以他为榜样.得努力.public class CustomAdapterActivity extends ListActivity{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(yout.main);ArrayList<Weather>weatherList = new ArrayList<Weather>();Weather w = new Weather( "London", 17, Weather.OVERCAST );weatherList.add( w );w = new Weather( "Paris", 22, Weather.OVERCAST );weatherList.add( w );w = new Weather( "Athens", 29, Weather.SUNNY );weatherList.add( w );w = new Weather( "Stockholm", 12, Weather.RAIN );weatherList.add( w );WeatherAdapterweatherAdapter = new WeatherAdapter(this,weatherList );setListAdapter( weatherAdapter );}}哎,这个大家都很清楚,关键问题是weatherAdapter哪来的呢?自己创建的啊,如果创建呢?public class WeatherAdapter extends BaseAdapter {private Context context;private List<Weather>weatherList; 这就是adapter关联的List,用来存储数据.还记的ArrayList 要往里传参数吗?传的也是这个类型啊.呵呵public WeatherAdapter(Context context, List<Weather>weatherList ) {this.context = context;this.weatherList = weatherList;}public intgetCount() {return weatherList.size();}public Object getItem(int position) {return weatherList.get(position);}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {Weather weather = weatherList.get(position);return new WeatherAdapterView(this.context, weather );}}哎,这段告诉了我们,有多少个Item,可以通过getCount()得到了。
Android适配器(Adapter)简单来说就是对数据和界⾯的适配。
⼀些不能直接赋值到界⾯上的数据类型(如List,Map等),就要靠适配器来展⽰到页⾯界⾯适配器控件⼯作流程:集合类数据对象 --> 适配器对象 --> 适配器控件1.ArrayAdapter主要⽤于纯⽂本数据的显⽰2.SimpleAdapter可⽤于复杂的数据显⽰,将集合中不同的数据项填充到不同的View的不同组件中3.BaseAdapter它是以上适配器的公共基类,可以实现以上适配器的所有功能,且可以⾃定义Adapter来定制每个条⽬的外观功能,具有较⾼的灵活性最终效果图:1.界⾯布局:说明:上⾯是约束布局组件,下⾯是ListView组件2.ListView组件中每⼀个item的布局3.为Item创建实体类public class QQMsgBean {private int qq_icon;private String qq_name;private String lastmsg_time;private String lasttitle;private String noetrendmsg_count;public QQMsgBean(int qq_icon, String qq_name, String lastmsg_time, String lasttitle, String noetrendmsg_count) { this.qq_icon = qq_icon;this.qq_name = qq_name;stmsg_time = lastmsg_time;sttitle = lasttitle;this.noetrendmsg_count = noetrendmsg_count;}//get,set⽅法....}4.创建⼀个适配器类该类继承BaseAdapter类,负责把数据和界⾯适配public class QQMsgAdapter extends BaseAdapter {private List<QQMsgBean> data; //数据private Context context; //上下⽂对象,⽅便getItem⽅法使⽤//构造器⽅法public QQMsgAdapter(List<QQMsgBean> data, Context context) {this.data = data;this.context = context;}@Overridepublic int getCount() {return data.size();}@Overridepublic Object getItem(int position) {return data.get(position);}@Overridepublic long getItemId(int position) {return position;}//获取每⼀⾏item的显⽰内容@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if(convertView == null){ //充分利⽤缓存机制提⾼效率convertView = LayoutInflater.from(context).inflate(yout.item_qqmessage,parent,false);}QQMsgBean bean = data.get(position); //通过索引获取对应的数据对象ImageView img = (ImageView)convertView.findViewById(R.id.img_qqicon);//获取view组件img.setImageResource(bean.getQq_icon()); //传⼊值给view组件TextView name = (TextView)convertView.findViewById(_qqname);name.setText(bean.getQq_name());TextView lasttitle = (TextView)convertView.findViewById(_lasttitle);lasttitle.setText(bean.getLasttitle());TextView tv_lastmsgtime = (TextView)convertView.findViewById(_lastmsgtime);tv_lastmsgtime.setText(bean.getLastmsg_time());TextView tv_notrendmsgcount = (TextView)convertView.findViewById(_notrendmsgcount); tv_notrendmsgcount.setText(bean.getNoetrendmsg_count());return convertView;}}5.Avtivity代码public class QQMessageActivity extends AppCompatActivity {private ListView lvMessage;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_qqmessage);lvMessage = (ListView)findViewById(R.id.lvMessage);List<QQMsgBean> data = getData(); //获取数据集合lvMessage.setAdapter(new QQMsgAdapter(data,this)); //把数据集合塞到适配器⾥⾯适配}public static List<QQMsgBean> getData(){}//获取数据集合的⽅法}。