package com.lidaochen.test; import android.support.v7.app.AppCompatActivity; 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 AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找到ListView控件 ListView listView = (ListView)findViewById(R.id.lv); // 准备ListView要显示的数据 List<Map<String, String>> data = new ArrayList<Map<String, String >>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("name", "张飞"); map1.put("phone", "13688888888"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("name", "马云"); map2.put("phone", "13777777777"); Map<String, String> map3 = new HashMap<String, String>(); map3.put("name", "刘强东"); map3.put("phone", "13222222222"); Map<String, String> map4 = new HashMap<String, String>(); map4.put("name", "马化腾"); map4.put("phone", "13666666666"); // 将map加入到集合中 data.add(map1); data.add(map2); data.add(map3); data.add(map4); // 创建一个SimpleAdapter SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"name", "phone"}, new int[]{R.id.tv_name, R.id.tv_phone}); // 设置适配器 listView.setAdapter(simpleAdapter); } }
<?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"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/tv_name" android:textSize="20sp" android:layout_weight="1" android:textColor="#d20606"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20sp" android:id="@+id/tv_phone" android:layout_weight="1"/> </LinearLayout>