学习Android,整理一下SimpleAdapter的知识,通过一个小例子来看具体代码(下图中的头像小图片请自行添加),
如下图:
1.0: 在layout文件夹下,新建list_view.xml文件
list_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/textview01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_toRightOf="@id/imageview" />
<TextView
android:id="@+id/textview02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_toRightOf="@id/imageview"
android:layout_below="@id/textview01" />
</RelativeLayout>
2.0: 在activity_main.xml文件中添加一个ListView控件
activity_main.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="com.lgl.simpleadapter.MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
3.0: 新建一个Person类
Person:
package com.lgl.simpleadapter;
public class Person {
private int id;
private String name;
private String desc;
public Person(int id, String name, String desc) {
super();
this.id = id;
this.name = name;
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
4.0: 在MainActivity中做代码实现
MainActivity:
package com.lgl.simpleadapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
listView = (ListView) findViewById(R.id.listview);
//创建数据源
Person person1 = new Person(R.drawable.f1, "JAVA", "小菜一碟!");
Person person2 = new Person(R.drawable.f2, "Android", "有点意思!");
Person person3 = new Person(R.drawable.f3, "SSH", "不在话下!");
Person person4 = new Person(R.drawable.f4, "JAVA EE", "手到擒来!");
Person[] persons = {person1, person2, person3, person4};
List<Map<String, Object>> list = new ArrayList<Map<String ,Object>>();
for (int i = 0; i < persons.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", persons[i].getId());
map.put("name", persons[i].getName());
map.put("desc", persons[i].getDesc());
list.add(map);
}
//创建适配器
SimpleAdapter adapter = new SimpleAdapter(
this, //当前上下文
list, //数据源
R.layout.list_view, //list_view布局文件
new String[] {"id", "name", "desc"}, //数据源Map的key键
new int[] {R.id.imageview, R.id.textview01, R.id.textview02} //key键对应的value值显示在哪个控件上
);
//绑定适配器
listView.setAdapter(adapter);
//绑定监听
listView.setOnItemClickListener(new SimpleAdapterTest());
}
//定义内部类,实现OnItemClickListener接口
class SimpleAdapterTest implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(MainActivity.this, "您选择了第" + arg2 + "行!", Toast.LENGTH_SHORT).show();
}
}
}
总结: 在Android中有多种适配器,比如:ArrayAdapter、SimpleAdapter、BaseAdapter、SimpleCursorAdapter等,它们就像连接数据源与UI界面之间的桥梁,不同适配器之间的区别在于它们所适配的数据源不同而已。