ListView 的简单使用day9android
功能:把数据绑定到ListView控件上
//数据有图片 姓名 年龄
//res/drawable-hdpi自行放入须要的图片ide
一、在res/layout布局界面有2个xml文件布局
//activity_main.xml文件 布局this
代码xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >事件
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>图片
</RelativeLayout>
------------------------get
//simplete_activity.xml文件 布局it
代码io
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
android:layout_alignTop="@+id/image"
android:text="name"
android:textSize="30sp"
/>
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_alignBottom="@+id/image"
android:text="age"
android:textSize="15sp" />
</RelativeLayout>
-----------------------
二、MainActivity 类
代码
public class MainActivity extends Activity {
private ListView listview;
private SimpleAdapter adapter;
//准备须要的数据
private String[] array_name = {"哈哈","呵呵","拉拉","嘻嘻","哒哒","嘎嘎","呜呜","喔喔","叽叽"};
private int[] array_age = {12,21,32,11,14,16,43,31,22};
private int[] array_images = {R.drawable.img01,R.drawable.img02,R.drawable.img03,
R.drawable.img04,R.drawable.img05,R.drawable.img06,
R.drawable.img07,R.drawable.img08,R.drawable.img09,};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listview = (ListView) this.findViewById(R.id.listview);
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
//利用循环 把须要的数据添加到一个集合
for(int i = 0;i<array_name.length;i++){
Map<String,Object> map = new HashMap<String, Object>();
map.put("head", array_images[i]);
map.put("name", array_name[i]);
map.put("age", array_age[i]);
data.add(map);
}
//new一个自定义适配器
//找到自定义的布局文件simplete_activity.xml文件
//而后把存入集合的数据丢入自定义适配器里
this.adapter = new SimpleAdapter(this, data, R.layout.simplete_activity,
new String[]{"head","name","age"}, new int[]{R.id.image,R.id.name,R.id.age});
//把自定义适配器绑定到ListView
listview.setAdapter(adapter);
//点击ListView的事件 监听
listview.setOnItemClickListener(new OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String, Object> select_item = (Map<String, Object>) adapter.getItem(position);//把点击到的 而后须要的数据设置到标题 setTitle(select_item.get("name").toString()); } }); }}