activity_adapter_fruit.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AdapterFruitActivity">
<ListView
android:id="@+id/listview_fruit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@color/red"
android:dividerHeight="3dp"/>
</LinearLayout>
listview_fruit.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AdapterFruitActivity">
<ListView
android:id="@+id/listview_fruit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@color/red"
android:dividerHeight="3dp"/>
</LinearLayout>
item_content.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FFFFFF"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:contentDescription="@string/blank"
android:id="@+id/item_img"
android:layout_height="60dp"
android:layout_width="100dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher_background" />
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<LinearLayout
android:layout_height="54dp"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView
android:gravity="bottom"
android:id="@+id/item_title"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_width="50dp"
android:text=""
android:textColor="#000"
android:textSize="12sp" />
<TextView
android:gravity="bottom"
android:id="@+id/item_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="50dp"
android:text=""
android:textColor="#000"
android:textSize="12sp" />
</LinearLayout>
<View
android:background="#AA388E91"
android:layout_height="6dp"
android:layout_width="match_parent" />
</LinearLayout>
</LinearLayout>
AdapterFruitActivity.java
package com.jld.myapplication;
import android.app.Activity;
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 AdapterFruitActivity extends Activity {
private ListView listView;
private List<Map<String, Object>> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adapter_fruit);
listView = findViewById(R.id.listview_fruit);
initDataList();
// key值数组,适配器通过key值取value,与列表项组件一一对应
String[] from = {"img", "price", "sales"};
// 列表项组件Id 数组
int[] to = {R.id.item_img, R.id.item_title, R.id.item_content};
/*
* SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
* context:activity界面类
* data 数组内容是map的集合数据
* resource 列表项文件
* from map key值数组
* to 列表项组件id数组 from与to一一对应,适配器绑定数据
*/
final SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.listview_item, from, to);
listView.setAdapter(adapter);
}
/**
* 初始化适配器需要的数据格式
*/
private void initDataList() {
//图片资源
int[] img = {
R.mipmap.ganlan,
R.mipmap.huolongguo,
R.mipmap.liulian,
R.mipmap.lizhi,
R.mipmap.longyan,
R.mipmap.mangguo,
R.mipmap.pingguo,
R.mipmap.putao,
R.mipmap.yezi,
R.mipmap.zao};
double[] price = {23.1, 56.2, 31.5, 42.3, 25.9, 45.8, 21.3, 32.7, 57.9, 62.3};
int[] sales = {52, 31, 14, 66, 98, 17, 32, 45, 64, 10};
list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", img[i]);
map.put("price", "¥" + price[i]);
map.put("sales", "已售" + sales[i] + "件");
list.add(map);
}
}
}