(1)数组适配器(ArrayAdapter)

ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(context, textViewResourceId, objects)

context:上下文对象,一般填this即可
textViewResourceId:每个Item的布局样式,可以直接使用android提供的(例如:android.R.layout.simple_list_item_1),如果android提供的不满足你的需求,你也可以自己写一个Layout
objects:数据源, 一般为一个数组或集合

实例:MainActivity.java
public class MainActivity extends Activity {    ListView listView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ListView) findViewById(R.id.listView);        ArrayAdapter adapter = new ArrayAdapter(this,                android.R.layout.simple_list_item_1, new String[] { "Item1",                        "Item2", "Item3", "Item4", "Item5" });        listView.setAdapter(adapter);    }}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ListView        android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="match_parent" >    ListView>RelativeLayout>

(2)简单适配器(SimpleAdapter)
它虽然叫简单适配器,其实它还是比较复杂的

SimpleAdapter adapter = new SimpleAdapter(context, data, resource, from, to);

context:上下文对象,一般填this即可
data: 数据源,一般为List包装的Map
resource:每个Item的布局样式
from: 一个String的数组,里面包含Map中的键名
to:绑定数据视图的中的ID,与from成对应的关系

实例:MainActivity.java
public class MainActivity extends Activity {    ListView listView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ListView) findViewById(R.id.listView);        SimpleAdapter adapter = new SimpleAdapter(this, getData(),                R.layout.item_layout, new String[] { "img", "text" },                new int[] { R.id.iv, R.id.tv });        listView.setAdapter(adapter);    }    //数据源    private List> getData() {        List> data = new ArrayList>();        for (int i = 0; i <= 5; i++) {            Map map = new ArrayMap();            map.put("img", R.drawable.ic_launcher);            map.put("text", "Item" + i);            data.add(map);        }        return data;    }}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ListView        android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="match_parent" >    ListView>RelativeLayout>

item_layout.xml

<?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/iv"        android:layout_width="60dp"        android:layout_height="60dp"/>    <TextView        android:id="@+id/tv"        android:layout_width="match_parent"        android:layout_height="60dp"        android:gravity="center_vertical"        android:textSize="40sp"/>LinearLayout>

注意:from 和 to 一定要是一一对应的

(3)基本适配器(BaseAdapter)
* 以后再更新*

更多相关文章

  1. [置顶] Android常用适配器控件
  2. Android UI设计——ListView控件与SimpleAdapter适配器(三)
  3. 浅析Android位置权限以及数组寻找索引的坑
  4. Android studio :适配器控件
  5. Android适配器之-----SimpleCursorTreeAdapter
  6. Android适配器之---SimpleCursorAdapter
  7. android之RecycleView适配器添加点击事件
  8. Android:自定义适配器

随机推荐

  1. Android(安卓)WebView总结
  2. android AlarmManager
  3. Android应用程序组件Content Provider的
  4. android shape
  5. android adb am命令
  6. Android定制出厂默认输入法
  7. Android(安卓)Non-UI to UI Thread Commu
  8. Android(安卓)让你的 EditText 只接受指
  9. android书籍
  10. android之实现各个组件点击事件监听