05-17 列表视图(2)

1.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.testapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" />
        <activity android:name=".test_activity6"></activity>
        <activity android:name=".TestActivity7">
        </activity>
        <activity android:name=".TestActivity8">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2.activity_test8.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:context=".TestActivity8">
 7 
 8     <ListView
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:id="@+id/lv_2">
12     </ListView>
13 </LinearLayout>

3.simple_adapter.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:orientation="horizontal"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent">
 7 
 8     <ImageView
 9         android:layout_width="100dp"
10         android:layout_height="100dp"
11         android:src="@drawable/f1"
12         android:id="@+id/iv_2"/>
13     <LinearLayout
14         android:layout_width="0dp"
15         android:layout_height="wrap_content"
16         android:orientation="vertical"
17         android:layout_weight="1"
18         android:layout_marginTop="20dp">
19         <TextView
20             android:layout_width="match_parent"
21             android:layout_height="wrap_content"
22             android:text="名字=aaa"
23             android:gravity="center"
24             android:id="@+id/tv_8"
25             android:textSize="20dp"
26             android:textColor="#F0F"/>
27         <TextView
28             android:layout_width="match_parent"
29             android:layout_height="wrap_content"
30             android:text="内容=aaa"
31             android:gravity="center"
32             android:id="@+id/tv_9"
33             android:textSize="20dp"
34             android:textColor="#F0F"/>
35     </LinearLayout>
36 </LinearLayout>

4.TestActivity8.java

  1 package com.example.administrator.testapp;
  2 
  3 import android.support.annotation.NonNull;
  4 import android.support.v7.app.AppCompatActivity;
  5 import android.os.Bundle;
  6 import android.widget.ListView;
  7 import android.widget.SimpleAdapter;
  8 
  9 import java.util.ArrayList;
 10 import java.util.Collection;
 11 import java.util.HashMap;
 12 import java.util.Iterator;
 13 import java.util.List;
 14 import java.util.ListIterator;
 15 import java.util.Map;
 16 import java.util.Objects;
 17 
 18 public class TestActivity8 extends AppCompatActivity {
 19     ListView lv_2;
 20 
 21     @Override
 22     protected void onCreate(Bundle savedInstanceState) {
 23         super.onCreate(savedInstanceState);
 24         setContentView(R.layout.activity_test_8);
 25 
 26         lv_2 = (ListView)findViewById(R.id.lv_2);
 27 
 28         //1-1.数据源集合
 29         List<Map<String,Object>> lm = new ArrayList<Map<String,Object>>();
 30 
 31         Map<String,Object> map = new HashMap<String,Object>();
 32         map.put("img",R.drawable.f1);
 33         map.put("name", "美食1");
 34         map.put("content", "美食1的介绍");
 35         lm.add(map);
 36 
 37         map = new HashMap<String,Object>();
 38         map.put("img",R.drawable.f2);
 39         map.put("name", "美食2");
 40         map.put("content", "美食2的介绍");
 41         lm.add(map);
 42 
 43         map = new HashMap<String,Object>();
 44         map.put("img",R.drawable.f3);
 45         map.put("name", "美食3");
 46         map.put("content", "美食3的介绍");
 47         lm.add(map);
 48 
 49         map = new HashMap<String,Object>();
 50         map.put("img",R.drawable.f4);
 51         map.put("name", "美食4");
 52         map.put("content", "美食4的介绍");
 53         lm.add(map);
 54 
 55         map = new HashMap<String,Object>();
 56         map.put("img",R.drawable.f5);
 57         map.put("name", "美食5");
 58         map.put("content", "美食5的介绍");
 59         lm.add(map);
 60 
 61         map = new HashMap<String,Object>();
 62         map.put("img",R.drawable.f6);
 63         map.put("name", "美食6");
 64         map.put("content", "美食6的介绍");
 65         lm.add(map);
 66 
 67         map = new HashMap<String,Object>();
 68         map.put("img",R.drawable.f7);
 69         map.put("name", "美食7");
 70         map.put("content", "美食7的介绍");
 71         lm.add(map);
 72 
 73         map = new HashMap<String,Object>();
 74         map.put("img",R.drawable.f8);
 75         map.put("name", "美食8");
 76         map.put("content", "美食8的介绍");
 77         lm.add(map);
 78 
 79         map = new HashMap<String,Object>();
 80         map.put("img",R.drawable.f9);
 81         map.put("name", "美食9");
 82         map.put("content", "美食9的介绍");
 83         lm.add(map);
 84 
 85         map = new HashMap<String,Object>();
 86         map.put("img",R.drawable.f10);
 87         map.put("name", "美食10");
 88         map.put("content", "美食10的介绍");
 89         lm.add(map);
 90 
 91 
 92         //数组 key的数组
 93         String [] strings = {"img","name","content"};
 94 
 95         int[]ids = {R.id.iv_2,R.id.tv_8,R.id.tv_9};
 96 
 97         //1-2.创建simple-AdaPter
 98 
 99         SimpleAdapter simpleAdapter = new SimpleAdapter(this,
100                 lm,R.layout.simple_adapter,strings,ids);
101 
102         //1-3.绑定适配器
103 
104         lv_2.setAdapter(simpleAdapter);
105 
106     }
107 }

原文地址:https://www.cnblogs.com/TENOKAWA/p/5503002.html