2.5.2 Adapter接口及实现类
来源:互联网 发布:ubuntu update rc.d 编辑:程序博客网 时间:2024/06/05 17:51
Adapter常用实现类:
1、ArrayAdapter:简单,易用的Adapter,通用用于将数组或List集合的多个值包装成多个列表项
2、SimpleAdapter:并不简单,功能强大的Adapter,可用于将List集合的多个对象包装成多个列表项。
3、SimpleCursorAdapter:与SimpleAdapter基本相似,只是用于包装Cursor提供的数据。
4、BaseAdapter:通常用于被扩展。
实例:使用ArrayAdapter创建ListView
<ListView android:id="@+id/list1" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView>
ListView list1 = (ListView) findViewById(R.id.list1); String[] arr1 = {"神灵武士","斧王","宙斯","白牛","蓝胖"}; //将数组包装成ArrayAdapter ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,arr1); //为ListView设置Adapter list1.setAdapter(adapter1);
实例:使用SimpleAdapter创建ListView
activity_main.xml
<ListView android:id="@+id/mylist" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView>
simple_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 定义一个ImageView,用于作为列表项的一部分。 --> <ImageView android:id="@+id/header" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 定义一个TextView,用于作为列表项的一部分。 --> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp"/> <!-- 定义一个TextView,用于作为列表项的一部分。 --> <TextView android:id="@+id/desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dp" android:paddingLeft="10dp"/> </LinearLayout></LinearLayout>
MainActivity.java
private String[] names = new String[]{"虎头","弄玉","李清照","李白"}; private String[] descs = new String[]{"可爱的女孩","一个擅长音乐的女孩","一个擅长文学的女性","浪漫主义诗人"}; private int[] imageIds = new int[]{R.drawable.tiger,R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个List集合,集合中的元素是Map List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>(); for (int i=0;i<names.length;i++){ Map<String,Object> listItem = new HashMap<String,Object>(); listItem.put("personName",names[i]); listItem.put("Header",imageIds[i]); listItem.put("desc",descs[i]); listItems.add(listItem); } //创建一个SimpleAdapter **SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{"personName", "Header","desc"},new int[]{R.id.name,R.id.header,R.id.desc});** ListView listView = (ListView) findViewById(R.id.mylist); listView.setAdapter(simpleAdapter);
上面程序的关键在于创建一SimpleAdapter,它需要5个参数,其中后面四个十分关键。
绑定单击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { System.out.println(names[position] +"被单击了!"); } });
绑定选中事件
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { System.out.println(names[position]+"被选中了!"); } @Override public void onNothingSelected(AdapterView<?> parent) { } });
0 0
- 2.5.2 Adapter接口及实现类
- Adapter接口及实现类
- adapter接口及实现类
- [Android]笔记14:Adapter接口及实现类
- Adapter接口及其实现类
- Adapter接口及其实现类
- Adapter接口及其实现类
- Android Adapter接口及实现类之ArrayAdapter、SimpleAdapter和BaseAdapter的简单介绍及运用
- Android Adapter接口的实现类总结
- 第19章:ListView和Adapter接口实现类
- Android中Adapter接口及其实现类详解
- EasyDemo*Adapter接口及其实现类体系结构图(on Github)
- list接口及实现类
- set接口及实现类
- Adapter接口
- UI组件之AdapterView及其子类关系,Adapter接口及其实现类关系
- BlockingQueue接口及实现类分析
- PHP 接口多继承 及 实现类转换成接口
- Fragment提换tabhost
- 剑指offer(17)-包含min函数的栈
- 技术人员的职业基石和发展方向
- 百度SDK定位,不用Application,用在自己的项目中,
- 开发App常见的九大错误
- 2.5.2 Adapter接口及实现类
- centos7+opencv3.1+libvlc
- 和为S的两个数字
- 关于Class.getResource和ClassLoader.getResource的路径问题
- linux tar指令常用选项
- 一、设计模式概述
- 将tomcat源码导入到Eclipse
- 艺术摄影--数码单反相机的基本操作和使用(2学时)--SDUST
- 八大排序算法