关于Android中适配器的使用简介
来源:互联网 发布:java todo用法 编辑:程序博客网 时间:2024/06/05 17:18
关于适配器的解释,简单的讲就是把要显示给用户的数据信息通过适当的模式动态的填充各种ListView,也可以看作是界面数据绑定的一种理解,它所操纵的数据一般都是一些比较复杂的数据,界面是有一定规律的View。由于一些特殊的需要,往往需要自己定义适配器来完成显示功能,下面就以较为常用的适配器简介其用法:
1、首先要定义一个用于显示ListView的布局文件main.xml,如下
- <?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"
- android:orientation="vertical" >
- <ListView
- android:id="@+id/person_list"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:drawSelectorOnTop="false"
- android:scrollbars="vertical" />
- </LinearLayout>
2、再定义一个ListView中要显示的内容的布局文件item.xml,如下
- <?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"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/user_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextView
- android:id="@+id/user_age"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
3、自定义一个适配器,使数据充填到对应的View中,该适配器继承自abstract类BaseAdapter
- package com.ui.adapter.demo;
- 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.TextView;
- public class PersonAdapter extends BaseAdapter
- {
- // 上下文对象
- private Context context;
- // 要绑定的数据
- private List<Person> persons;
- // 自定义的内部类
- private ViewHolder holder;
- // 布局文件加载器
- private LayoutInflater inflater;
- public PersonAdapter(Context context, List<Person> persons)
- {
- super();
- this.context = context;
- this.persons = persons;
- }
- /*取得绑定的数据的个数*/
- @Override
- public int getCount()
- {
- return persons.size();
- }
- /*取得指定位置的对象*/
- @Override
- public Object getItem(int position)
- {
- return persons.get(position);
- }
- /*取得绑定数据的索引*/
- @Override
- public long getItemId(int position)
- {
- return position;
- }
- /*取得指定位置的View*/
- @Override
- public View getView(int position, View convertView, ViewGroup parent)
- {
- holder = null;
- // 重生之大文豪www.dwhao.com获取加载器对象
- inflater = LayoutInflater.from(context);
- if (null == convertView)
- {
- // 加载布局文件
- convertView = inflater.inflate(R.layout.person, null);
- holder = new ViewHolder();
- // 获取布局文件中的各控件
- holder.nameText = (TextView) convertView
- .findViewById(R.id.user_name);
- holder.ageText = (TextView) convertView
- .findViewById(R.id.user_age);
- convertView.setTag(holder);
- }
- else
- {
- convertView.getTag();
- }
- // 给控件绑定数据
- holder.nameText.setText(persons.get(position).getName());
- holder.ageText.setText(persons.get(position).getAge());
- return convertView;
- }
- /**
- * 自定义内部类,类似于缓存的作用
- */
- private class ViewHolder
- {
- TextView nameText;
- TextView ageText;
- }
- }
0 0
- 关于Android中适配器的使用简介
- 关于Android中适配器的使用简介
- Android中适配器的使用
- Android中关于PopupWindow的使用简介
- Android中关于SharedPreferences的使用简介
- Android中关于TabHost的使用简介
- 【Android】 Android中适配器简介
- Android中自定义适配器的使用
- Android中适配器Adapter的使用
- android中适配器关于notifyDataSetChange
- Android中关于自定义Dialog的使用简介
- Android中关于Options Menu的使用简介
- Android适配器的使用
- android 适配器的使用
- android适配器的使用
- Android中ListView,Gallery,GridView 的适配器Adapter的使用
- 关于android中主题的简介
- android之ListView中使用的适配器CursorAdapter
- iOS学习笔记--图片灰度处理
- Mininet Documents In Chinese
- 正则表达式
- StringBuffer
- word自动生成目录如何从正文第一页开始
- 关于Android中适配器的使用简介
- GC之CMS GC日志分析
- JS中数组的操作
- Git 基础学习
- 关于send时产生WSAEWOULDBLOCK问题
- oracle把一个表的数据复制到另一个表中
- [整理]ISO 27001信息安全管理体系审核员试题汇编之单项选择题
- Android Jsoup 爬取网页数据
- cocos2d-js中关于继承的细节