Android开发之适配器

来源:互联网 发布:淘宝网 医用弹力祙 编辑:程序博客网 时间:2024/06/05 02:23

android提供了的两种适配器,ArrayAdapter和SimpleAdapter

1.ArrayAdapter

package com.linxiaosheng.listview;import com.linxiaosheng.R;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;public class MyListView extends Activity {private ArrayAdapter<String> arrayAdapter;//private SimpleAdapter simpleAdapter;private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.listview);listView=(ListView)findViewById(R.id.listView1);/** <span style="white-space:pre"></span>*context:上下文<span style="white-space:pre"></span>* resource:数据视图<span style="white-space:pre"></span>* 数据源:数组或list集合 <span style="white-space:pre"></span>*/<span style="white-space:pre"></span>String arr[]={"广州","深圳"};<span style="white-space:pre"></span>arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr );<span style="white-space:pre"></span>listView.setAdapter(arrayAdapter);}}
完成效果:


2.SimpleAdapter

package com.linxiaosheng.listview;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.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;import com.linxiaosheng.R;public class MyListView extends Activity {private SimpleAdapter simpleAdapter;private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.listview);listView=(ListView)findViewById(R.id.listView1);/**<span style="white-space:pre"></span>* context:上下文<span style="white-space:pre"></span>* data:数据源(List<? extends Map<String,Object>>data)一个map组成的list集合<span style="white-space:pre"></span>* <span style="white-space:pre"></span>每一个map的键值对都会去对应listview的一行 <span style="white-space:pre"></span>* <span style="white-space:pre"></span>每一个map键值对中的健必须包含在from数组中所指定的健<span style="white-space:pre"></span>* resource:列表项的布局文件ID<span style="white-space:pre"></span>* form:map中的健名 <span style="white-space:pre"></span>* to:绑定数据视图的ID,与forom成对应关系<span style="white-space:pre"></span>*/<span style="white-space:pre"></span>simpleAdapter=new SimpleAdapter(this, getSimpleAdapterData(), R.layout.simplelistview,new String[]{"name","age"}, new int[]{R.id.imageView1,R.id.textView1});<span style="white-space:pre"></span>listView.setAdapter(simpleAdapter);}public List<Map<String,Object>> getSimpleAdapterData(){List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();for(int i=0;i<10;i++){Map<String,Object> map=new HashMap<String, Object>();map.put("name", R.drawable.ic_launcher);map.put("age", 22);list.add(map);}return list;}}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="简单适配器"        android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>

完成效果:



0 0
原创粉丝点击