Android中Adapter之BaseAdapter使用
Android界面中有时候需要显示稍微复杂的界面时,就需要我们自定义一个adapter,而此adapter就要继承BaseAdapter,重新其中的方法.
<!-- 第一个LinearLayout,充当父容器,要包揽两行,因此设置其 android:orientation为 纵向-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- 第二个LinearLayout
android:gravity 表示该LinearLayout中的类容相对于该LinearLayout 水平居中
android:layout_gravity 表示的是该LinearLayout相对于它上面的父容器(这里是第一个LinearLayout)水平居中
-->
<LinearLayout android:id="@+id/toprow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<TextView android:id="@+id/quna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff9966ff"
android:textSize="25dp"
android:text="@string/places"/>
<!-- 用一个图片按钮 -->
<ImageButton android:id="@+id/goBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/quna"/>
</LinearLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/placesList"
android:textColor="#ff9966ff"
android:textSize="20dp"/>
<ListView android:id="@+id/list_places"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" //此属性的设置非常重要,决定里面的控件横向摆放
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- android:layout_margin设置图片与旁边文章的距离 -->
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
<LinearLayout android:orientation="vertical" //也很重要决定里面的两个textview竖着放
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff3399ff"
android:textSize="20dp"/>
<TextView android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff3399ff"
android:textSize="13dp"/>
</LinearLayout>
<!-- 注意此处button的属性android:layout_gravity的用法 ,这里设置了都没效果
这里不是很理解为什么要在加一个LinearLayout,里面的button才可以居右??
而且LinearLayout的width必须为fill_parent才可以
-->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button android:id="@+id/viewBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_select"
android:layout_gravity="bottom|right"/>
</LinearLayout>
</LinearLayout>
private ImageButton goBtn;
private List<Map<String, Object>> testData;
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_header);//这里运行该项目的时候,让其显示listview_header.xml界面(layout),然后再将listview要显示的item项加到里面
//获得listview_header.xml中的ListView控件
//获得imageButton给它添加click事件
goBtn = (ImageButton) findViewById(R.id.goBtn);
goBtn.setOnClickListener(new OnClickListener() {
//这里只是做出了简单的弹出框
@Override
public void onClick(View v) {
new AlertDialog.Builder(BaseAdapterTest2.this).setTitle("想去的国家:")
}
});
//数据源
testData = buildData();
MyAdapter adapter = new MyAdapter(this);
listView.setAdapter(adapter);
}
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("info", "China");
map.put("image", R.drawable.p6);
data.add(map);
map = new HashMap<String, Object>();
map.put("title", "英国");
map.put("info", "England");
map.put("image", R.drawable.p7);
data.add(map);
map = new HashMap<String, Object>();
map.put("title", "美国");
map.put("info", "America");
map.put("image", R.drawable.p9);
map = new HashMap<String, Object>();
map.put("title", "荷兰");
map.put("info", "Dutch");
map.put("image", R.drawable.p9);
data.add(map);
map = new HashMap<String, Object>();
map.put("title", "新西兰");
map.put("info", "New Zealand");
map.put("image", R.drawable.p7);
data.add(map);
return data;
//构造函数:要理解(这里构造方法的意义非常强大,你也可以传一个数据集合的参数,可以根据需要来传参数)
public MyAdapter(Context context){
this.inflater = LayoutInflater.from(context);
}
//这里的getCount方法是程序在加载显示到ui上时就要先读取的,这里获得的值决定了listview显示多少行
@Override
public int getCount() {
//在实际应用中,此处的返回值是由从数据库中查询出来的数据的总条数
return testData.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return this.testData.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//有很多例子中都用到这个holder,理解下??
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
//把vlist layout转换成View【LayoutInflater的作用】
convertView = inflater.inflate(R.layout.vlist, null);
//通过上面layout得到的view来获取里面的具体控件
holder.image = (ImageView) convertView.findViewById(R.id.image);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.info = (TextView) convertView.findViewById(R.id.info);
holder.viewBtn = (Button) convertView.findViewById(R.id.viewBtn);
//不懂这里setTag什么意思??
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
//这里testData.get(position).get("title1")),其实就是从list集合(testData)中取出对应索引的map,然后再根据键值对取值
holder.image.setBackgroundResource((Integer) testData.get(position).get("image1"));
holder.title.setText((String) testData.get(position).get("title1"));
holder.info.setText((String) testData.get(position).get("info1"));
//为listview上的button添加click监听
holder.viewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
.....
}
});
return convertView;
}
}

如果屏幕移动了之后,并且导致某些item项跑到屏幕外面,此时如果还有新的item需要产生,则这些item显示时调用的getView方法中的
convertview就不为null,而是那些移出到屏幕之外的view,我们所要做的就是将需要显示的item项填充到移除屏幕外的(旧的)view
中去,注意【convertview为null的不仅仅是初始化显示的那些item,还有一些是已经开始移入屏幕但还没有view被回收的那些
item】。

Android中Adapter之BaseAdapter使用的更多相关文章
- Android中Adapter总结
根据一个制作列表的程序开始练手,结果就出现了学习安卓的第一个代码问题 运行程序发现,虽然报错,但是可以成功运行程序. Android中Adapter功能为 显示ListView,最常用的有ArrayA ...
- Android中Adapter和Bridge模式理解和应用
一 Adapter模式 意图: 将一个类的接口转换成客户希望的另外一个接口. Adapter模式使得原本由于接口不兼容而不能在一起工作的那些类可以在一起工作. 适用性: 使用一个已存在的类,而它的接口 ...
- android 中listview之BaseAdapter的使用
Listview控件不像其他安卓控件那种直接拖拽到界面上就能用,而是采用类似J2EE中的MVC模型的方式使用,需要通过适配器将某种样式的数据或控件添加到其上而使用. MVC模型实现原理是 数据模型M( ...
- Android中ListView通过BaseAdapter实现数据的绑定
1. public class ListFiles extends Activity { ListView Listview=null; protected void onCreate(Bundle ...
- android中Adapter适配器的讲解
Adapter(适配器的讲解) 适配器就我自己来看,我觉得这是一个非常重要的知识点,Adapter是用来帮助填出数据的中间桥梁,简单点说吧:将各种数据以合适的形式显示在View中给用户看.Adapte ...
- Android 中万能的 BaseAdapter(Spinner,ListView,GridView) 的使用!
大家好!今天给大家讲解一下BaseAdapter(基础适配器)的用法,适配器的作用主要是用来给诸如(Spinner,ListView,GridView)来填充数据的.而(Spinner,ListVie ...
- Android中Adapter类的使用 “Adapter”
Adapter用来把数据绑定到扩展了AdapterView类的视图组(例如:ListView或Gallery).Adapter负责创建代表所绑定父视图中的底层数据的子视图. 可以创建自己的Adapte ...
- Android中的自定义Adapter(继承自BaseAdapter)——与系统Adapter的调用方法一致——含ViewHolder显示效率的优化(转)
Android中很多地方使用的是适配器(Adapter)机制,那我们就要好好把这个Adapter利用起来,并且用出自己的特色,来符合我们自行设计的需要喽~~~ 下面先上一个例子,是使用ViewHold ...
- 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...
随机推荐
- Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法
Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法 Some sites could ...
- 关于谷歌浏览器不能播放背景音乐的问题(与IE的不同之处)
第一篇博文 忍受寂寞,抵制诱惑,持之以恒. 开发时,以下代码在IE浏览器上能顺利播放背景音乐,可在谷歌浏览器上却没有动静. <html> <body> <bgsound ...
- elasticsearch的mapping映射
Mapping简述 Elasticsearch是一个schema-less的系统,但并不代表no shema,而是会尽量根据JSON源数据的基础类型猜测你想要的字段类型映射.Elasticsearch ...
- ECMAScript 6学习笔记(二):let和块级作用域
同步发布于:https://mingjiezhang.github.io/(转载请说明此出处). ES6中加入了let,也让JavaScript拥有了块级作用域. 没有块级作用域的JavaScript ...
- <input type="text"/>未输入时属性value的默认值--js学习之路
在百度ife刷题是自己的一个错误引发了我对<input type="text"/>的学习. 先贴代码: <!DOCTYPE html> <html&g ...
- 关于ArcGIS10.0中的栅格计算中的函数
版本升级确实很重要,在ArcGIS10.1中计算成功的,在10.0中出了问题. 问题 在进行栅格计算时,计算公式很简单,包括两个Ln函数: "-22.73 + 11.1 * Ln(5) + ...
- 【转】C++的拷贝构造函数深度解读,值得一看
建议看原帖 地址:http://blog.csdn.net/lwbeyond/article/details/6202256 一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很 ...
- 全球最低功耗蓝牙单芯片DA14580的软件体系 -层次架构和BLE消息事件处理过程
在作者之前发表的<全球最低功耗蓝牙单芯片DA14580的系统架构和应用开发框架分析>.<全球最低功耗蓝牙单芯片DA14580的硬件架构和低功耗>.<全球最低功耗蓝牙单芯片 ...
- Android线程池(一)
本篇文章主要介绍Android自带的线程池的使用. 首先要引入线程池的概念 线程池:是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务. 线程池线程都是后台线程.每个线 ...
- iOS之UI--主流框架的搭建--仿制QQ的UI框架
使用XCode搭建多个控制器界面,一般在实际开发中建议超过四个控制器界面使用纯代码. 下面的实例其实已经超过了四个,总结详细步骤的目的,主要是更熟悉XCode的StoryBoard使用细节. 先直接上 ...