android ListView 示例4 使用 SimpleAdapter 创建 ListView

下面的xml文件中定义了一个ListView,ListView中将会显示由SimpleAdapter提供的列表项。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

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

接下来是在之前的代码基础上修改而来的Activity的代码。

package com.toby.personal.testlistview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

    private String[] array1 = new String[]{"C", "C++", "Qt", "QML"};
    private String[] array2 = new String[]{"001", "002", "003", "004"};
    private int[] imageIds = new int[]{R.drawable.dog_001, R.drawable.dog_002
            , R.drawable.dog_003, R.drawable.dog_004};

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

        List<Map<String, Object>> listItems = new ArrayList<>();

        for (int i = 0; i < array1.length; ++i) {
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("array1", array1[i]);
            listItem.put("array2", array2[i]);
            listItem.put("image", imageIds[i]);
            listItems.add(listItem);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.simple_item,
                new String[]{"array1", "array2", "image"},
                new int[]{R.id.array1, R.id.array2, R.id.image});

        ListView listView = (ListView) findViewById(R.id.myList);

        listView.setAdapter(simpleAdapter);
    }
}

R.layout.simple_item对应的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/array1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#f0f"
        />

    <TextView
        android:id="@+id/array2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        />
</LinearLayout>

我们可以通过为ListView添加监听器的方式来监听ListView的列表项的事件

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.d(TAG, array1[i] + " is clicked.");
            }
        });

参考文献:《疯狂Android讲义(第2版)》

    原文作者:赵者也
    原文地址: https://www.jianshu.com/p/1b70a02aeef1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞