java适配器各三种_适配器三种
适配器有:
作用:用于将数据绑定到组件上
过程:写的内容----->适配器
控件------------------->通过适配器----->listView的布局
1,ArrayAdapter列表适配器
2,SimpleCursorAdapter简单标游适配器
3,BaseAdapter基础适配器
(2,3都可以用复杂的Lietview)
1,ArrayAdapter列表适配器(只能写TextView体系的控件)
java代码写:
package com.example.layou_text;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class CopyOfdi1Activity extends Activity {
ListView lv_man;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lv_man);//加载(是布局不是id!)
lv_man=(ListView)findViewById(R.id.lv_man);//获取操作
// 注意写法不能写R.layout.获取布局
//准备数据
String [] data={"a","b","c","d","e","f","g","h"};
//构造arrayaadapter适配(上下文,加载另一个布局id,传入数据(数据会被传入布局中))
ArrayAdapter adapter = new ArrayAdapter(CopyOfdi1Activity.this,
R.layout.item_array_adapter,data);
// 加载textview体系的单个布局
//给lv_man设置适配器
lv_man.setAdapter(adapter);
}}
xml:(定义一个listview布局)id+lv_man.xml
android:id="@+id/lv_man"
android:layout_width="match_parent"
android:layout_height="match_parent" >
再写一个xml(定义listView里面的内容textview)
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:gravity="start"
>
ArrayAdapter效果图
image.png
2,SimpleAdapter简单的适配器
java写:
package com.example.layou_text;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class CopyOfdi2Activity extends Activity {
ListView lv_man;
List data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lv_man);//加载(是布局不是id!)
lv_man=(ListView) findViewById(R.id.lv_man);//获取操作
ArrayList> arrayList = new ArrayList>();
//new了一个Arraylist数组包含map集合
HashMap hashMap = new HashMap();
//new了一个map集合编写key value
hashMap=new HashMap();
//简写用上面hashMap的对象再新建一个map集合,赋值,添加到arrayList集合中
hashMap.put("icom", R.drawable.f2);
hashMap.put("name","美食--2");
hashMap.put("content","内容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f3);
hashMap.put("name","美食--3");
hashMap.put("content","内容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f4);
hashMap.put("name","美食--4");
hashMap.put("content","内容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f5);
hashMap.put("name","美食--5");
hashMap.put("content","内容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f6);
hashMap.put("name","美食--6");
hashMap.put("content","内容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f7);
hashMap.put("name","美食--7");
hashMap.put("content","内容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f8);
hashMap.put("name","美食--8");
hashMap.put("content","内容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f9);
hashMap.put("name","美食--9");
hashMap.put("content","内容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f10);
hashMap.put("name","美食--10");
hashMap.put("content","内容");
arrayList.add(hashMap);
//arrayList集合里面有许多个map集合,map集合中有许多的数据
// Map对象中的key的数组,用于得到对应的value
String from[]={"icom","name","content"};//定义from代表map里面的key值
// item布局文件中的子View的id的数组
int [] to ={R.id.imageView1,R.id.tv_item_name,R.id.tv_item_content,};
//定义to代表布局控件
// 准备SimpleA
SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList,
R.layout.activity_chatn, from, to);
//加载包含to的控件视图(不是视图id),from是通过key获取了value值,
// to是把value赋给了布局中的控件
lv_man.setAdapter(simpleAdapter);
// 设置适配器
}
}
lv_man.xml布局写
android:id="@+id/lv_man"
android:layout_width="match_parent"
android:layout_height="match_parent" >
再写一个xml(定义listView里面的内容控件)
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center_vertical"
android:src="@drawable/f1" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
>
android:id="@+id/tv_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:id="@+id/tv_item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
/>
效果图
image.png
3,BaseAdapter基础适配器(需要继承是抽象类)
java写
package com.example.layou_text;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView lv_man;//列表视图
List data;//不用map用了shoinfo类来储存数据集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lv_man);//加载(是布局不是id!)
lv_man=(ListView) findViewById(R.id.lv_man);//获取操作
// 准备集合数据
data =new ArrayList();
data.add(new shoinfo(R.drawable.f1,"美食名--1","美食内容--1"));
data.add(new shoinfo(R.drawable.f2,"美食名--2","美食内容--2"));
data.add(new shoinfo(R.drawable.f3,"美食名--3","美食内容--3"));
data.add(new shoinfo(R.drawable.f4,"美食名--4","美食内容--4"));
data.add(new shoinfo(R.drawable.f5,"美食名--5","美食内容--5"));
data.add(new shoinfo(R.drawable.f6,"美食名--6","美食内容--6"));
data.add(new shoinfo(R.drawable.f7,"美食名--7","美食内容--7"));
data.add(new shoinfo(R.drawable.f8,"美食名--8","美食内容--8"));
data.add(new shoinfo(R.drawable.f9,"美食名--9","美食内容--9"));
data.add(new shoinfo(R.drawable.f10,"美食名--10","美食内容--10"));
// 准备Baseadapter对象
Myadapter myadapter = new Myadapter();
//设置Adapter显示列表
lv_man.setAdapter(myadapter);
}
class Myadapter extends BaseAdapter{//定义一个局部内部类继承BaseAdapter
//返回集合的总数
@Override
public int getCount() {
return data.size();
}
//返回指定下标对应的数据对象
@Override
public Object getItem(int position) {
return data.get(position);
}
//返回每个条目的ID
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//返回指定下标所对应的Item的View对象(试图和总数)
// position下标
// convertView可以复用的缓存Item视图对象, 前n+1个为null
// parent listView对象
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1.创建或得到对应当前行的一个Viewholder对象
ViewHolder hole=null;
if(convertView==null){
// 加载布局Item的布局,得到View对象
convertView = View.inflate(MainActivity.this, R.layout.activity_chatn, null);
hole=new ViewHolder();
// 获取到convertView的子view的控件
hole.findViewById = (ImageView) convertView.findViewById(R.id.imageView1);
hole.view = (TextView) convertView.findViewById(R.id.tv_item_name);
hole.view1 = (TextView) convertView.findViewById(R.id.tv_item_content);
// 将holder对象保存到converterView上
convertView.setTag(hole);
}else{
hole=(ViewHolder)convertView.getTag();
}
// 根据position设置对应的数据
// 得到当行的数据对象
shoinfo shoinfo = data.get(position);
// 得到子View对象
// 给ViewHolder对象的视图设置数据
hole.findViewById.setImageResource(shoinfo.getIncon());
hole.view.setText(shoinfo.getName());
hole.view1.setText(shoinfo.getContent());
return convertView;
}
//数据类
class ViewHolder{
public ImageView findViewById;
public TextView view;
public TextView view1;
}
}
}
java写一个存放数据类
package com.example.layou_text;
public class shoinfo {
public int incon;
public String name;
public String content;
@Override
public String toString() {
return "shoinfo [incon=" + incon + ", name=" + name + ", content="
+ content + "]";
}
public int getIncon() {
return incon;
}
public void setIncon(int incon) {
this.incon = incon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public shoinfo(int incon, String name, String content) {
super();
this.incon = incon;
this.name = name;
this.content = content;
}
}
定义一个listview(id+lv_man)
android:id="@+id/lv_man"
android:layout_width="match_parent"
android:layout_height="match_parent" >
再写一个xml(定义listView里面的内容控件)
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/f1" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
>
android:id="@+id/tv_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:id="@+id/tv_item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
/>
效果图
image.png
java适配器各三种_适配器三种相关推荐
- java中国象棋绝杀算法_象棋30种绝杀秘籍大全,GIF图解灵活运用可百战百胜,绝无敌手...
象棋文化源远流长,象棋有各种各样的杀法,古人根据各杀法的特点分别给起了很多有趣的名字,很多杀法我们都用过多次,具体名字还不知道,下面就学习下各种杀法的命名吧. 1.钓鱼马用马在对方三.三或七.三位置上 ...
- java shape 空间操作类_设计三个类:几何形状类(Shape)、圆类(Circle)、矩形类(.,请编写 Java程序,设计一个“形状rdquo...
问题标题 设计三个类:几何形状类(Shape).圆类(Circle).矩形类(.,请编写 Java程序,设计一个"形状&rdquo 2019-5-10来自ip:17.175.174. ...
- java.lang.object源码_第三篇:java.lang.Object 类源码分析
Object所包含的方法如下: ① public Object(); 构造函数: 大部分情况下,类对象的声明,都是通过构造函数完成的(Java中规定:在类定义过程中,对于未定义构造函数的类,默认会有一 ...
- java 德生读卡器对接程序_第三章:java流程语句
一,java语句结构: 1.顺序结构 2.选择结构 3.循环结构 二,选择语句: if,if else ,if else if,switch 1.if语句(单分支): if(boolean类型条件表达 ...
- java 获取打印机缺纸_第三章软件
试题列表 题号 科目 题 型 题干 难 度 分 类 1413 计算 机基 础 判 断 存储在磁盘中的 MP3 音乐. JPEG 图片等都是计算机软件. 答案: N 中 1417 计算 机基 础 判 断 ...
- java连接销售订单查询_(三十一)订单管理-查询订单
查询订单: 所有的订单 不区分用户 基本的sql select* from orders where 1=1判断是否有state 若有则添加state 最后order by ordertime des ...
- java分词主谓宾_英语五种结构的句子(主谓 主谓宾 主谓宾宾补 主系表 主谓双宾)谁给我讲一下…...
共回答了23个问题采纳率:100% 英语中的五种基本句型结构 一.句型1:Subject (主语) + Verb (谓语) 这种句型中的动词大多是不及物动词,所谓不及物动词,就是这种动词后不可以直接接 ...
- 一款三搭_奔三奔四的女人别乱穿,这5件裙子再火都别穿,丑得像地摊货
女人上了年纪以后对衣着打扮可以说要更加的上心,因为不可能再像年轻的时候什么都往身上穿,到了一定的年龄段,女人既要选择适合自己的衣服,又要选择显气质显年轻的穿搭.这不小编今天要给大家带来的就是,奔三奔四 ...
- mysql三范式和反三范式_数据库三范式和反三范式
要说数据库什么最抽象,我觉得就是这个三范式,不是很好理解,但是表在设计的时候又必须要知道这么一个规则. 首先使用最简洁的话说说这三范式: 第一范式(1NF:The First Normal Form) ...
最新文章
- Linux常用命令——hostname
- Magento如何自定义404页面?
- JavaBean中的属性
- tcp长连接和短连接的区别_TCP --- 连接
- 《影视特效镜头跟踪技术精粹(第2版)》——导读
- Little Elephant and Shifts(CF-220C)
- can't get master address from zookeeper /新旧数据不一致
- 《http权威指南》阅读笔记(六)
- Java 基础 之 变量
- windows下 gcc 下载及使用指南
- 【Markdown小技巧】 整理小图标和表情符号
- word里如何设置目录页码
- 最全的smarty‘总结~耿兴隆老师著作,学子zyf献上
- mysql em_Python在主体Emai中发送MySQL查询
- 制作QQ背景音乐链接
- smarty-wap端
- TensorFlow使用Keras Tuner自动调参
- Java 八大基本数据类型简述
- android+捕获google账户+cancel按钮,MVVM: 这是一个android MVVM 框架,基于谷歌dataBinding技术实现...
- GPU呈现模式分析中颜色的意义
热门文章
- Windows窗口分析
- php mysql_fetch_array mysql_fetch__php提示Warning:mysql_fetch_array() expects的解决方法
- python简介pdf_PDFMiner首页、文档和下载 - Python PDF 解析器 - OSCHINA - 中文开源技术交流社区...
- win7怎么合并计算机窗口,win7 已设置任务栏窗口从不合并,游戏多开,如何固定窗口顺序?...
- pd 生成mysql 脚本_PowerDesigner 如何生成数据库更新脚本
- 趣学python3(40)--TCP服务器和客户端(socketserver类)
- 【机器学习】6大监督学习模型:毒蘑菇分类
- 【深度学习】使用深度学习阅读和分类扫描文档
- 【Python】在模仿中精进数据可视化09:近期基金涨幅排行可视化
- 【Python】全面掌握Python基础,这一篇就够了,建议收藏