2.5.2 Adapter接口及实现类

Adapter常用实现类:
1、ArrayAdapter:简单,易用的Adapter,通用用于将数组或List集合的多个值包装成多个列表项
2、SimpleAdapter:并不简单,功能强大的Adapter,可用于将List集合的多个对象包装成多个列表项。
3、SimpleCursorAdapter:与SimpleAdapter基本相似,只是用于包装Cursor提供的数据。
4、BaseAdapter:通常用于被扩展。

实例:使用ArrayAdapter创建ListView

    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">        
    ListView>
 ListView list1 = (ListView) findViewById(R.id.list1);
        String[] arr1 = {"神灵武士","斧王","宙斯","白牛","蓝胖"};
        //将数组包装成ArrayAdapter
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,arr1);
        //为ListView设置Adapter
        list1.setAdapter(adapter1);

实例:使用SimpleAdapter创建ListView

activity_main.xml

    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    ListView>

simple_item.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <ImageView android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"/>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <TextView android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="#f0f"
            android:paddingLeft="10dp"/>
        
        <TextView android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:paddingLeft="10dp"/>
    LinearLayout>
LinearLayout>

MainActivity.java

 private String[] names = new String[]{"虎头","弄玉","李清照","李白"};
    private String[] descs = new String[]{"可爱的女孩","一个擅长音乐的女孩","一个擅长文学的女性","浪漫主义诗人"};
    private int[] imageIds = new int[]{R.drawable.tiger,R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //创建一个List集合,集合中的元素是Map
     List> listItems = new ArrayList>();
        for (int i=0;i listItem = new HashMap();
            listItem.put("personName",names[i]);
            listItem.put("Header",imageIds[i]);
            listItem.put("desc",descs[i]);
            listItems.add(listItem);
        }

        //创建一个SimpleAdapter
        **SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{"personName",
        "Header","desc"},new int[]{R.id.name,R.id.header,R.id.desc});**

        ListView listView = (ListView) findViewById(R.id.mylist);
        listView.setAdapter(simpleAdapter);

上面程序的关键在于创建一SimpleAdapter,它需要5个参数,其中后面四个十分关键。

绑定单击事件

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                System.out.println(names[position] +"被单击了!");
            }
        });

绑定选中事件

 listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                System.out.println(names[position]+"被选中了!");
            }

            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });

你可能感兴趣的:(Android)