ListView-simpleAdapter

样式图+工程结构图

 

main-xml

<?xml version="1.0" encoding="utf-8"?>
<ListView
    android:id="@+id/lv_main_show"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>

item-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="match_parent"
    android:orientation="horizontal">
<ImageView
    android:id="@+id/iv_simple_icon"
    android:layout_width="70dp"
    android:layout_height="90dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:orientation="vertical"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical">
        <TextView
            android:id="@+id/tv_simple_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_simple_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

Acivity中代码

package com.example.listview;

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 MainActivity_simpleadapter extends AppCompatActivity {

    private ListView lv_main_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_main_show = findViewById(R.id.lv_main_show);

        List<Map<String, Object>> data = new ArrayList<>();
        Map<String, Object> mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f1);
        mapdate1.put("name", "name--1");
        mapdate1.put("price", "price--1");
        data.add(mapdate1);
        mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f2);
        mapdate1.put("name", "name--2");
        mapdate1.put("price", "price--2");
        data.add(mapdate1);
        mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f3);
        mapdate1.put("name", "name--3");
        mapdate1.put("price", "price--3");
        data.add(mapdate1);
        mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f4);
        mapdate1.put("name", "name--4");
        mapdate1.put("price", "price--4");
        mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f5);
        mapdate1.put("name", "name--5");
        mapdate1.put("price", "price--5");
        data.add(mapdate1);
        mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f6);
        mapdate1.put("name", "name--6");
        mapdate1.put("price", "price--6");
        data.add(mapdate1);
        mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f7);
        mapdate1.put("name", "name--7");
        mapdate1.put("price", "price--7");
        data.add(mapdate1);
        mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f8);
        mapdate1.put("name", "name--8");
        mapdate1.put("price", "price--8");
        data.add(mapdate1);
        mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f9);
        mapdate1.put("name", "name--9");
        mapdate1.put("price", "price--9");
        data.add(mapdate1);
        mapdate1 = new HashMap<>();
        mapdate1.put("icon", R.drawable.f10);
        mapdate1.put("name", "name--10");
        mapdate1.put("price", "price--10");
        data.add(mapdate1);
        String[] from = {"icon", "name", "price"};
        int[] to = {R.id.iv_simple_icon, R.id.tv_simple_name, R.id.tv_simple_price};
        SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.simpleadapter_layout,from,to);
        lv_main_show.setAdapter(adapter);

    }
}

猜你喜欢

转载自blog.csdn.net/we1less/article/details/107736729
今日推荐