Android适配器Adapter学习
在开发中我们需要绑定一些数据展现到桌面上,这是就需要AdapterView。AdapterView是ViewGroup的子类,它决定了怎么展现视图通过Adapter来绑定特殊的数据类型。AdapterView是非常有帮助的当你展现数据在你的布局中。Gallery,ListView和Spinner是AdapterView的子类。
下面看一下AdapterView的结构图:
然后再看一下Adapter的结构图:
上面已经充分展现了他们的子类和父类的基础关系。
下面我们看一个ListViewDemo的例子:
先来看一个简单的adapter的例子:
public class SimpleList extends ListActivity {
private String[] mListString={"姓名:王魁锋","性别:男","年龄:23",
"居住地:上海市普陀区","邮箱:wangkuifeng0118@126.com"};
private ListView mListView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mListView=this.getListView();
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.易做图_list_item_1,mListString));
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(SimpleList.this, "你选择了:"+mListString[position], 1).show();
}
});
}
这里用到了系统定义好的适配模式,当然这只能用来简单的数据适配,下面看一下效果:
接下来看一个稍微复杂点的,SimpleAdapter怎么适配:
public class IconList extends ListActivity {
private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"};
private String[] mListStr = { "王魁锋", "男", "23", "上海市普陀区",
"wangkuifeng0118@126.com"};
ListView mListView = null;
ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
mListView = getListView();
int lengh = mListTitle.length;
for(int i =0; i < lengh; i++) {
Map<String,Object> item = new HashMap<String,Object>();
item.put("image", R.drawable.portrait);
item.put("title", mListTitle[i]);
item.put("text", mListStr[i]);
mData.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.iconlist,
new String[]{"image","title","text"},new int[]{R.id.image,R.id.title,R.id.text});
setListAdapter(adapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
&
补充:移动开发 , Android ,