Android中使用自定义Adapter
来源:互联网 发布:矩阵讲解 编辑:程序博客网 时间:2024/04/29 04:06
我在刚玩android 时候,对这个adapter很不理解,到底是什么原理呢?适配器,哎,只知道setAdapter()把参数传进去,系统就显示出来了。
今天针对这个东西,我们做个系统详细的分析。listview加载adapter过程是这样的.
1 先判断adapter 有多少数据项,根据这个数据确定有多少item.
2 确定每个item里加载哪个View.
3 把View里加载要显示的数据.
问提一个一个来解决.
第一个问题:
因为adapter都要关联一个list.有来存储数据.list的项数就是Item的数目.我们在重载BaseAdapter 时候,都要实现这个函数
public int getCount() { return weatherList.size(); }
哎,这个函数就是确定关联条目的.
第二个问题:
哪来的view呢?当然我们自己创建的.重载BaseAdapter时候你要实现getView()这个函数,就是这个view.
第三个问题:
你自己创建的view.加载哪些数据你该知道的.呵呵.
张豪就喜欢看例子,这个小伙子技术,管理都很牛,得以他为榜样.得努力.
public class CustomAdapterActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.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 ); WeatherAdapter weatherAdapter = 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 int getCount() { 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()得到了.可是View 哪来的呢?
当然是getView()这个函数提供.
这个view 的获取就多中多样了,我们可以传个LayoutID. 通过Inflater出来,也可以自己创建个,只要出来就行.
在这里,我们自己创建个View. 这个View.是个VIewGroup.
class WeatherAdapterView extends LinearLayout { public static final String LOG_TAG = "WeatherAdapterView"; public WeatherAdapterView(Context context, Weather weather ) { super( context ); this.setOrientation(HORIZONTAL); LinearLayout.LayoutParams cityParams = new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT); cityParams.setMargins(1, 1, 1, 1); TextView cityControl = new TextView( context ); cityControl.setText( weather.getCity() ); addView( cityControl, cityParams); LinearLayout.LayoutParams temperatureParams = new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT); temperatureParams.setMargins(1, 1, 1, 1); TextView temperatureControl = new TextView(context); temperatureControl.setText( Integer.toString( weather.temperature ) ); addView( temperatureControl, temperatureParams); LinearLayout.LayoutParams skyParams = new LinearLayout.LayoutParams(25, LayoutParams.WRAP_CONTENT); ImageView skyControl = new ImageView( context ); Log.d( LOG_TAG, weather.getCity()+" -> "+weather.sky ); skyControl.setImageResource( weather.getSkyResource() ); addView( skyControl, skyParams ); } }
有了这个就很清楚了.呵呵,很明白吧,一定得深入,细致的理解.
原创地址没找着,不好意思,就没贴了。
- Android中使用自定义Adapter
- Android中使用自定义Adapter(一)
- Android中使用自定义Adapter(二)
- Android中使用自定义Adapter(一)
- Android中使用自定义Adapter(二)
- Android 中自定义 Adapter
- Android 中自定义 Adapter
- android使用自定义的adapter
- Android中自定义Adapter和ListView
- Android中自定义Adapter的基本原理
- Android 自定义Adapter中实现startActivityForResult
- Android中自定义数据适配器Adapter
- Android中自定义Adapter的基本原理
- Android学习笔记--自定义Adapter的使用
- android自定义adapter和bundle的使用
- Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)
- Android中Spinner下拉列表(使用自定义Adapter实现,显示数据取自联系人数据库)
- Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现) .
- 最有前途的php框架
- 用Repeater做的留言板的分页功能
- setjmp和longjmp的"非本地跳转"
- Spring AOP 详解
- Android平台中应该如何获取音乐文件的信息
- Android中使用自定义Adapter
- (求解)weblogic-ejb-eclipse/toplink配置下的问题
- freemarker记录
- Rich Communication Suite
- kvm 和 qemu的关系
- svn 文件状态标记含义 常见符号
- Qt 设置菜单过长时向上滚动
- Maven实战——常用Maven插件介绍
- STM32系列微控制器开发工具与应用——keil