Android Adapter的一些记录
一、摘要
An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.
这是Android官方对Adapter的定义。这个关系可以描述为下面一个关系图,

二、Adapter的继承关系
![]()
三,样例
这里附上北航《Android编程宝典》的一个例子,搭配ListView说明Adapter怎么样去扮演这个中间人,为View层提供数据。
1,定义3个layout文件,分别是activity_main.xml,fragment_main.xml,my_listitem.xml。
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="so.byj.example.mytestapp.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout01"
android:baselineAligned="false"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <ListView android:id="@+id/myListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView> </LinearLayout>
my_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="30sp" /> <TextView android:id="@+id/itemText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/> </LinearLayout>
2,编写MainActivity,代码如下
package so.byj.example.mytestapp; import java.util.ArrayList;
import java.util.HashMap; import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.os.Build; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} /**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); ListView list = (ListView) rootView.findViewById(R.id.myListView); ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for(int i=0;i<30;i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "This is Title.....");
map.put("ItemText", "This is text.....");
mylist.add(map);
} SimpleAdapter mSchedule = new SimpleAdapter(getActivity(),
mylist,
R.layout.my_listitem, // 这里定义了数据填充的样式
new String[] {"ItemTitle", "ItemText"},
new int[] {R.id.itemTitle,R.id.itemText}); list.setAdapter(mSchedule); return rootView;
}
} }
如上面黑体部分,先把待填充数据的ListView拿出来,然后实例一个Adapter对ListView进行数据填充。
Android Adapter的一些记录的更多相关文章
- android布局常用属性记录
android布局常用属性记录 http://blog.csdn.net/xn4545945/article/details/7717086这里有一部分别人总结的其余的: align:对齐 par ...
- Android进阶(十四)Android Adapter详解
Android Adapter详解 Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View.因为layout五花八门,很多数据都不能直接绑定上去, ...
- android studio 使用问题记录
android studio 使用问题记录 下载地址:FQ或园子内好心人提供的国内地址: http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/an ...
- [转]Android Adapter以及getView()方法的理解
Android Adapter基本理解: 我的理解是: 1.一个有许多getter的类(就是getView(),getCount()....这些方法) 2.有多少个get方法?都是什么? 这些gett ...
- Android中的路径记录
Android中的路径记录 | RobinBlog 导航 导航 博客 分类 标签 友链 关于 搜索 Environment.getDataDirectory().getPath()=/dataEnvi ...
- Android Adapter基本理解
感谢大佬:https://blog.csdn.net/l799069596/article/details/47301711 Android Adapter基本理解: 我的理解是: 1.一个有许多ge ...
- Android开发入门要点记录:四大组件
cocos2dx跨平台开发中需要了解android开发,昨天快速的浏览了一本Android开发入门教程,因为之前也似懂非懂的写过Activity,Intent,XML文件,还有里面许多控件甚至编程思想 ...
- Android——Adapter
Adapter——本身只是一个接口.Adapter是将数据绑定到UI界面上的桥接类.Adapter负责创建显示每个项目的子View和提供对下层数据的访问. 数据适配器作用:把复杂的数据(数组.链表.数 ...
- Android adapter适配器的学习
学习Android有一点时间,说说自己的学习感悟. 首先呢,先说说适配器的作用,顾名思义,它就是把数据定义好一定的规则,使得我们可以用到ListView GridView等上面 所以说这玩意,还是得好 ...
随机推荐
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 导航页的开发--手机web app开发笔记
好了,的所有的基础知识已经准备完毕了,现在开始制作引导页.这个引导页需要一个HTML文件,JS文件,一个CSS文件.在HBuilderX中根目录下添加“Guid.html”,在JS文件夹添加“myth ...
- MSIL实用指南-生成break和continue
break和continue本质都是标签的跳转
- Python--高阶函数、函数嵌套、名称空间及变量作用域、闭包、装饰器
1.高阶函数(map/reduce/filter) 高阶函数是指函数的参数可以是函数 这篇总结几个常用的高阶函数:map/reduce/filter map函数.reduce函数.filter函数都是 ...
- C#开发BIMFACE系列1 BIMFACE 简介
系列目录 [已更新最新开发文章,点击查看详细] BIMFACE 是什么 BIMFACE = 国内领先的BIM轻量化引擎 BIMFACE 是广联达公司旗下的一款具有完全自主知识产权的BIM轻量化 ...
- jasypt
jasypt-1.9.0.jar import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; public class EncryptUt ...
- 2019 Multi-University Training Contest 8
2019 Multi-University Training Contest 8 C. Acesrc and Good Numbers 题意 \(f(d,n)\) 表示 1 到 n 中,d 出现的次数 ...
- codeforces 862 C. Mahmoud and Ehab and the xor(构造)
题目链接:http://codeforces.com/contest/862/problem/C 题解:一道简单的构造题,一般构造题差不多都考自己脑补,脑洞一开就过了 由于数据x只有1e5,但是要求是 ...
- Python字符串中删除特定字符
分析 在Python中,字符串是不可变的.所以无法直接删除字符串之间的特定字符. 所以想对字符串中字符进行操作的时候,需要将字符串转变为列表,列表是可变的,这样就可以实现对字符串中特定字符的操作. 1 ...
- CVE-2014-6271 Shellshock 破壳漏洞 复现
补坑. 什么是shellshock ShellShock是一个BashShell漏洞(据说不仅仅是Bash,其他shell也可能有这个漏洞). 一般情况来说,系统里面的Shell是有严格的权限控制的, ...