SimpleAdapter:可显示文字加图片
activity_activitysimple.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="120dp" > <ImageView android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/anniu5" android:layout_gravity="center_vertical" android:id="@+id/iv_1"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="生命可贵" android:textSize="20dp" android:id="@+id/tv_1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="安全出口" android:textSize="20dp" android:id="@+id/tv_2"/> </LinearLayout> </LinearLayout>
Activitysimple.java
package com.example.chenshuai.test321; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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 Activitysimple extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activitysimple); ListView simple_1 = (ListView)findViewById(R.id.simple_1); //准备数据源 List<Map<String,Object>> im = new ArrayList<Map<String,Object>>(); Map<String,Object> map = new HashMap<String,Object>(); map.put("image",R.drawable.anniu3); map.put("name","安全出口1"); map.put("content","保护生命1"); im.add(map); map = new HashMap<String,Object>(); map.put("image",R.drawable.anniu4); map.put("name","安全出口2"); map.put("content", "保护生命2"); im.add(map); map = new HashMap<String,Object>(); map.put("image",R.drawable.anniu5); map.put("name","安全出口3"); map.put("content", "保护生命3"); im.add(map); map = new HashMap<String,Object>(); map.put("image",R.drawable.anniu6); map.put("name","安全出口4"); map.put("content", "保护生命4"); im.add(map); map = new HashMap<String,Object>(); map.put("image",R.drawable.anniu7); map.put("name","安全出口5"); map.put("content", "保护生命5"); im.add(map); map = new HashMap<String,Object>(); map.put("image",R.drawable.anniu8); map.put("name","安全出口6"); map.put("content", "保护生命6"); im.add(map); map = new HashMap<String,Object>(); map.put("image",R.drawable.anniu9); map.put("name","安全出口7"); map.put("content", "保护生命7"); im.add(map); map = new HashMap<String,Object>(); map.put("image",R.drawable.anniu10); map.put("name","安全出口8"); map.put("content", "保护生命8"); im.add(map); //1.数据源里key的数组 String str[] = {"image","name","content"}; //2.layout文件里面子视图的id key与value相对应 int[] viewid = {R.id.iv_1,R.id.tv_1,R.id.tv_2}; SimpleAdapter simpleAdapter = new SimpleAdapter(this,im,R.layout.simple_layout,str,viewid); simple_1.setAdapter(simpleAdapter); } }