使用SimpleAdapter创建ListView
先定义界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.cao156.simpleadapter.MainActivity">
<!--定义一个ListView-->
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
MainActivity代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
private String[] names = new String[]{"虎头", "弄玉", "李清照", "李大白"};
private String[] descs = new String[]{"可爱的小爱", "一个擅长音乐的女孩", "一个擅长文学的女性", "浪漫主义诗人"};
private int[] imagesIds = 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集合,List集合的元素是Map
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("header", imagesIds[i]);
listItem.put("personName", names[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 list = (ListView) findViewById(R.id.mylist);
list.setAdapter(simpleAdapter);
}
}
使用SimpleAdapter最难的在于创建SimpleAdapter对象,它需要5个参数,其中后面4个参数十分关键。
第二个参数:该参数应该是一个List<?extends Map<String,?>>
类型的集合对象,该集合中每个Map<String,?>
对象生成一个列表项。
第三个参数:该参数指定一个界面布局的ID。例如,此处指定了R.latout.simple_item,这意味着使用app\src\main\res\layout\simple_item.xml,作为列表项组件。
第四个参数:该参数应该是一个String[]类型的参数,该参数决定提取Map<String,?>
对象中哪些key对应value来生成列表项。
第五个参数:该参数应该是一个int[]类型的参数,该参数决定填充那些组件。
R.layout.simple_item对应的布局文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--定义一个ImageView,用于作为列表的一部分-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/header"
android:paddingLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--定义一个ImageView,用于作为列表的一部分-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:textSize="20dp"
android:textColor="#f0f"
android:paddingLeft="10dp"/>
<!--定义一个ImageView,用于作为列表的一部分-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/desc"
android:textSize="14dp"
android:paddingLeft="10dp"/>
</LinearLayout>
</LinearLayout>