2014-10-25Android学习--------SimpleAdapter的实现

来源:互联网 发布:spss软件应用例子 编辑:程序博客网 时间:2024/04/30 10:41

我学习Android都是结合源代码去学习,这样比较直观,非常清楚的看清效果,觉得很好,今天的学习源码是网上找的个HealthFood 源码 百度搜就知道很多下载的地方

本篇学习需要结合前面写的几篇文章,特别是关于布局学习(三) 地址:http://blog.csdn.net/u014737138/article/details/40480291

如何要对ListView进行数据绑定,必须使用到一个接口:Adapter。

其中最经常与ListView进行配合使用的有ArrayAdapter、 CursorAdapter及SimpleAdapter等。

从名称可以看出ArrayAdapter使用的是一个ArrayAdapter做为数据源,SimpleCursorAdapter使用的是一个Cursor使用数据源,都比较容易理解,那么如何使用SimpleAdapter作为数据的适配器呢。

首先看下源代码:

@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.list);List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();for (int i = 0; i < resId.length; i++) {Map<String, Object> map = new HashMap<String, Object>();map.put("ImageView01", resId[i]);map.put("TextView01", food[i]);map.put("TextView02", food1[i]);            lists.add(map);}SimpleAdapter adapter = new SimpleAdapter(this, lists, R.layout.list_view_row, new String[]{"ImageView01","TextView01", "TextView02"}, new int[]{R.id.ImageView01, R.id.TextView01, R.id.TextView02});setListAdapter(adapter);listView = this.getListView();listView.setOnItemClickListener(this);}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {// TODO Auto-generated method stubList<Map<String, Object>> data = new ArrayList<Map<String,Object>>();for (int j = 0; j < efood.length; j++) {Map<String, Object> map = new HashMap<String, Object>();map.put(efood[j], efoodinfo[j]);data.add(map);}Intent intent = new Intent();intent.setClass(FoodListView.this, FoodInfo.class);intent.putExtra("drawable", resId[position]);intent.putExtra("foodname", food[position]);intent.putExtra("efoodnema", food1[position]);intent.putExtra("foodinfo", foodjianjie[position]);startActivity(intent);}  

首先这个activity是继承ListActivity的:public class FoodListView extends ListActivity implements OnItemClickListener

它实现OnItemClickListener 是因为它采用了这种监听事件的方法:

listView.setOnItemClickListener(this);

code:listView = this.getListView();//注意到这里的listView不是通过this.findViewByID得到控件对象的,而是系统自己本身就可以找到这个控件,所以这里要求的就是setContentView(R.layout.list);这行代码对应的list.xml文件只有一个ListView控件,唯一一个


接下来我们看看构造函数:SimpleAdapter(this, lists, R.layout.list_view_row, new String[]{"ImageView01","TextView01", "TextView02"}, new int[]{R.id.ImageView01, R.id.TextView01, R.id.TextView02});

它的原型是这样的:

Open Declarationandroid.widget.SimpleAdapter.SimpleAdapter
Public Constructors
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
Parameters
context:The context where the View associated with this SimpleAdapter is running  当前正在运行的activity上下文,并且它与适配的视图有关
data: A List of Maps. Each entry in the List corresponds to one row in the list. 数组的每一个实体对象跟list中的每一行需要相匹配的
The Maps contain the data for each row, [map]中包含listView控件每一行所需要的数据
and should include all the entries specified in "from" //data中应该包含所有的在参数“form”具体的实例对象
resource: Resource identifier of a view layout that defines the views for this list item.//定义的布局文件的id号,这个布局文件定义了
ListView控件的每一行
 The layout file should include at least those named views defined in "to"//并且这个布局文件中必须包含int[]数组参数中所代表的所有
元素与之对应的子控件id号
from: A list of column names that will be added to the Map associated with each item.//一个数组,这个数组里面的内容与ListView的每一
行的值相关的,有联系的
to: The views that should display column in the "from" parameter. 视图布局文件中应该显示from参数对应的个数的id号,
These should all be TextViews. 这些值代表的应该都是TextView类型的控件
The first N views in this list are given the values of the first N columns in the from parameter.一 一 对应

其实说简单一点:from 和 to 两个数组参数,个数是一样的,即数组的长度相同,int[]数组里面各个元素值是item布局文件中含有的所有的id号
 from[]数组里面的元素是int[]数组元素相对应的id所代表的控件的内容,resource代表的是item布局文件的资源id号,在R.java
中可以找到。context代表上下文,也就是当前的activity代表的上下文,this。data代表一个实体对象类的数组。这个数组里面放着
的内容就是String数组里面的所有元素对应的键值,并且给这些键值一个value值。

那么现在我们看下代码中是怎么做的:
SimpleAdapter adapter = new SimpleAdapter(this, lists, R.layout.list_view_row, new String[]{"ImageView01","TextView01", "TextView02"}, new int[]{R.id.ImageView01, R.id.TextView01, R.id.TextView02});

this:代表的当前的FoodListView   activity类
lists申明是:List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
它是一个数组,数组元素是一个Map对象,Map对象里面放置的是一个Stirng键值+Object值对
它与List<? extends Map<String, ?>> 要求是一致的
R.layout.list_view_row:它是一个布局文件的id号,
new String[]{"ImageView01","TextView01", "TextView02"}:from参数数组里面放置的是三个元素:图片视图,文本视图,文本视图的id号
int[]{R.id.ImageView01, R.id.TextView01, R.id.TextView02}:int参数数组里面放置的是从资源文件中获得的,项目中所有资源的索引id,他们分别对应着from参数

接下来我们再去看看lists是怎么初始化的:
for (int i = 0; i < resId.length; i++) {//resId是什么?它是所有要显示的食物图片的名称id组成的数组
Map<String, Object> map = new HashMap<String, Object>();//申明一个map对象
map.put("ImageView01", resId[i]);//向map里面放值,第一个键值为ImageView01,它的值为  resId[i].
map.put("TextView01", food[i]);//向map里面放值,第二个键值为TextView01,它的值为  food[i].
map.put("TextView02", food1[i]);//向map里面放值,第三个键值为TextView02,它的值为  food1[i].
          lists.add(map);//然后将map加到lists数组里面
}

看看resId的初始化:
private static final int[] resId = { R.drawable.pork, R.drawable.pigliver, R.drawable.pigblood,
R.drawable.lamb, R.drawable.beef, R.drawable.beefliver,
R.drawable.goose, R.drawable.rabbit, R.drawable.dog,
R.drawable.duck, R.drawable.chicken, R.drawable.donkey,
R.drawable.egg, R.drawable.carp, R.drawable.yellowfish,
R.drawable.shrimp, R.drawable.shrimp2, R.drawable.crab,
R.drawable.clam, R.drawable.turtle, R.drawable.riversnail,
R.drawable.garlic, R.drawable.onion, R.drawable.radish,
R.drawable.celery, R.drawable.leek, R.drawable.spinach,
R.drawable.lettuce, R.drawable.bamboo, R.drawable.tomato,
R.drawable.foreignonion, R.drawable.vinegar, R.drawable.tea,
R.drawable.beanmilk, R.drawable.brownsuger, R.drawable.honey,
R.drawable.milk, R.drawable.whitespirit, R.drawable.beer };

它就是代表我们要显示的所有的图片,也就ListView每一行要放置的图片,resId[i]就代表着每一行的图片
这个图片我们把它放在一个map对象里面,给它设置的键值为:map.put("ImageView01", resId[i]);,也就是说通过ImageView01键值取出来的东西就是我们要显示的图片,只不过这个东西代表的是图片资源的id索引号。

那么ListView每一行要放置的除了图片之外,还有两个文本,这个可以从页面效果上面看出来,
所以这个时候我们需要把那两个文本也需要加到map对象中去,所以就有这样的设置:
map.put("TextView01", food[i]);//也就是说通过TextView01键值取出来的值就是我们要显示的第一个文本框的内容
map.put("TextView02", food1[i]);也就是说通过TextView02键值取出来的值就是我们要显示的第二个文本框的内容

那么接下来又对food[i[]和food1[i]初始化即可:
private static final String[] food = { "猪肉", "猪肝", "猪血", "羊肉", "牛肉", "牛肝", "鹅肉", "兔肉", "狗肉",
"鸭肉", "鸡肉", "驴肉", "鸡蛋", "鲤鱼", "黄鱼", "虾", "虾皮", "螃蟹", "蛤", "鳖肉",
"田螺", "大蒜", "葱", "萝卜", "芹菜", "韭菜", "菠菜", "莴笋", "竹笋", "西红柿", "洋葱",
"醋", "茶", "豆浆", "红糖", "蜂蜜", "牛奶", "白酒", "啤酒" };
每一个food[i]对应的元素必须要与resId[i]保持一致,同理food1也是一样的,这里就不介绍了

只不过这里需要注意的是:这种写法不好,应该初始化是需要去解析xml文件最好,

到这里我们的SimpleAdapter就构造成功了,接下来就是激活它,只需要一行代码就可以:
setListAdapter(adapter);
这行代码就是对ListView控件进行事件的匹配,

再调用监听事件:
listView.setOnItemClickListener(this);去实现它未实现的方法就可以了:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();//
for (int j = 0; j < efood.length; j++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(efood[j], efoodinfo[j]);
data.add(map);
}

Intent intent = new Intent();
intent.setClass(FoodListView.this, FoodInfo.class);
intent.putExtra("drawable", resId[position]);
intent.putExtra("foodname", food[position]);
intent.putExtra("efoodnema", food1[position]);
intent.putExtra("foodinfo", foodjianjie[position]);
startActivity(intent);


}  

这个函数在的作用再下节里面再讲,

到此,ListView布局就成功实现了。

主要使用的方法上就是SimpleAdapter



0 0
原创粉丝点击