layout-activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bluelesson.my_listview.MainActivity">
<ListView
android:id="@+id/list_item"
android:divider="#f50909"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_title"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标题"/>
<TextView
android:id="@+id/item_size"
android:textSize="15sp"
android:text="大小"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/item_version"
android:textSize="15sp"
android:text="版本"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
MainActivity:
package com.example.bluelesson.my_listview;
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);
//1.获取listview的对象
ListView listView = findViewById(R.id.list_item);
//2.准备数据源
// 2.1 图片
// 2.2 文字
List<Map<String,Object>> mapLists = new ArrayList<>();
Map<String,Object> maps = new HashMap<>();
maps.put("logo",R.mipmap.ic_launcher);
maps.put("title","网易云音乐");
maps.put("version","1.0");
maps.put("size","45.24MB");
Map<String,Object> maps2 = new HashMap<>();
maps2.put("logo",R.mipmap.ic_launcher);
maps2.put("title","酷狗音乐");
maps2.put("version","1.4");
maps2.put("size","34.24MB");
Map<String,Object> maps3 = new HashMap<>();
maps3.put("logo",R.mipmap.ic_launcher);
maps3.put("title","QQ音乐");
maps3.put("version","2.0");
maps3.put("size","67.24MB");
mapLists.add(maps);
mapLists.add(maps2);
mapLists.add(maps3);
//3.创建适配器
SimpleAdapter simpleAdapter= new SimpleAdapter(this,
mapLists, //数据源
R.layout.item, //布局文件
new String[]{"logo","title","version","size"}, //与数据源对应的key
new int[]{R.id.item_logo,
R.id.item_title,
R.id.item_version,
R.id.item_size}); //要显示的每个控件的ID,与第四个参数对应
// 4.关联适配器
listView.setAdapter(simpleAdapter);
}
}