您的位置:首页 > 移动开发 > Android开发

第四章:android核心组件之SimpleAdapter适配器构造函数的详解(二)

2016-04-17 17:36 507 查看
package com.example.simpleadapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
//【1】首先,为ListViw列表控件定义一个相应的----列表控件变量
//【1】其次,再为响应ListView列表项单击事件的TextView文本框控件定义一个相应的----变量
private ListView lv;
private TextView txtNotice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//【2】将ListView列表控件与相应的控件变量关联起来
//【2】将TextView文本框控件与相应的变量也关联起来
txtNotice=(TextView) findViewById(R.id.txtNotice);
lv=(ListView) findViewById(R.id.lv);
//【3】定义一个ArrayList集合,用来存储ListView列表控件中每一个列表项 的数据内容
ArrayList<HashMap<String,Object>> users=new ArrayList<HashMap<String,Object>>();
//【4】向ArrayList集合数组中,添加数据
for(int i=0;i<10;i++){
//【1】首先,每循环一次,就创建并且实例化一个HashMap类的对象
//【1】每一个HashMap类的对象,就相当于一个数组,每一个数组中,以键值对的形式存储三个元素,图片,名字,年龄
HashMap<String,Object> user=new HashMap<String, Object>();
//【2】通过HashMap中的成员方法put()向集合中以键值对--key--->value的形式向集合中添加数据
user.put("img",R.drawable.img1);
user.put("username", "名字("+i+")");
user.put("age", (11+i)+"");
//【3】然后,通过ArrayList集合类中的成员方法add()将HashMap类的这个实例化对象添加到集合ArrayList集合中
users.add(user);
}
//【5】将SimpleAdapter适配器与进行数据绑定
SimpleAdapter saImageItems=new SimpleAdapter(this,
users,                                   //数据来源,需要绑定的数据
R.layout.user,                           //每一个user.xml相当于ListView中的一个组件
new String[]{"img","username","age"},    //分别对应value的id
new int[]{R.id.img,R.id.txtName,R.id.txtAge}
);
//【7】通过ListView列表控件类中的成员方法setAdapter(),将列表控件与SimpleAdapter适配器进行绑定
lv.setAdapter(saImageItems);
//【8】为ListView中的每一列表项设置事件监听器
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
txtNotice.setText("你点击了第:"+position+"行!");
}
});
}
}
/********************************************************************************
* 1--使用SimpleAdapter适配器的数据一般都是由--HashMap构成的列表,列表的每一节,对应ListView中的每一行
* 2--通过SimpleAdapter的构造函数,将HashMap每个键的数据映射到布局文件中所对应的控件上
* 3--这个布局文件一般根据自己的需要来自己定义
* ******************************************************************************/
/*************************【梳理一下使用SimpleAdapter适配器的步骤】***********************
* 1--根据需要定义ListView每行所需要实现的布局
* 2--定义一个HaspMap所构成的列表,将以键值对的方式存储在里面
* 3--构造SimpleAdapter适配器的对象
* 4--将ListView绑定到SimpleAdapter适配器上
* ******************************************************************************/


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/img">
</ImageView>

<TextView
android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"
/>

<TextView
android:id="@+id/txtAge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"
/>
</TableRow>

</TableLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: