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;
}
}
转载于:https://www.cnblogs.com/xiadongqing/p/5638897.html
Android中Adapter之BaseAdapter使用相关推荐
- android中Adapter适配器的讲解
android中Adapter适配器的讲解 Adapter(适配器的讲解) 适配器就我自己来看,我觉得这是一个非常重要的知识点,Adapter是用来帮助填出数据的中间桥梁,简单点说吧:将各种数据以合适 ...
- Android中Adapter适配器的介绍以及用法的总结
Adapter(适配器的讲解) 适配器就我自己来看,我觉得这是一个非常重要的知识点,Adapter是用来帮助填出数据的中间桥梁,简单点说吧:将各种数据以合适的形式显示在View中给用户看.Adapte ...
- Android中Adapter的notifyDataSetInvalidated()和notifyDataSetChanged()的区别
notifyDataSetChanged方法通过一个外部的方法控制如果适配器的内容改变时需要强制调用getView来刷新每个Item的内容. public void notifyDataSetChan ...
- Android中内存优化
CSDN博客不写,排名会下降,我知道了...... Android内存优化,设计到很多方面,参考别大神的博客,自己也总结一下..... 下面将通过两篇博客,浅析Android 中的内存优化问题.来张图 ...
- Android中关于Adapter的使用(下)BaseAdapter
我们在前面三篇文章分别介绍了ArrayAdapter和SimpleAdapter的使用,可以先总结一下: 1)ArrayAdapter,是一个跟Array结构对应的Adapter,所以它展示的内容取决 ...
- Android中的各种Adapter
1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都需要用到Adapter.如下图直 ...
- Android学习四、Android中的Adapter
一.Adapter的介绍 An Adapter object acts as a bridge between an AdapterView and the underlying data for t ...
- Android中的Adapter
Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View 因为layout五花八门,很多数据都不能直接绑定上去,所以Android引入了Adapt ...
- android 中自定义安装,Android开发中ListView自定义adapter的封装
[引入] 我们一般编写listView的时候顺序是这样的: •需要展示的数据集List •为这个数据集编写一个ListView •为这个ListView编写一个Adapter,一般继承自BaseAda ...
- android getitem,android中Baseadapter的 getItem 跟 getItemId 的作用和重写
android中Baseadapter的 getItem 和 getItemId 的作用和重写 重写Baseadapter时,我们知道需要重写以下四个方法:getCount,getItem(int p ...
最新文章
- winform自动更新之AutoUpdater.NET
- Java Web项目,Android和微信小程序的初始页面配置
- 时序数据库连载系列:指标届的独角兽Prometheus
- wpf异常:指定的 Visual 不是此 Visual 的上级问题处理解析
- python3.6安装步骤-Ubuntu16.04安装python3.6详细教程
- 栈的应用(进制转换)
- 【ACL2020】详解基于显式语义分解的词典释义生成
- 4.ES 相关插件安装
- mysql blob类型图片输出到前端
- CDR中神奇的卷页滤镜
- 重标极差分析法matlab,重标极差(RS)分析法估计Hurst指数的有效性检验.doc
- elasticsearch 数据类型
- 2.1 八边形绘制
- java基本数据类型Char
- 【笔记】CMake构建C++工程
- 移动互联网十年内难以盈利 莫盲目跟风
- iframe 的使用理解
- Python分布式搜索引擎-10章elasticsearch入门
- java汉字转拼音(全拼、简拼)
- EDIUS中怎么实现卡拉OK字幕的制作
热门文章
- 区块链 Scilla是什么
- Superset集成到iframe 跨域 无需账号登录
- docker容器中bash: vi: command not found
- 单片机4层电梯设计c语言,详解基于单片机的四层电梯控制系统的设计与实现,Proteus仿真和程序代码...
- atlas 力矩计算_Atlas 2.1.0 实践(2)—— 安装Atlas
- Java中递归复制多级文件夹(IO流)
- 拉取 google.golang.org/grpc 报错
- VBS 控制 Windos 系统音量 及视频播放
- 使命召唤ol显示服务器超时,使命召唤ol网络延迟高的完整解决方案
- python如何制作一个任意列表_我要悄悄学Python之列表(一)