Android——SimpleAdapter适配器的使用(配合listview使用,显示图片内容和文本内容)

实现方案1:SimpleAdapter适配器

listView_Activity:

package com.example.myapplication_one;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class listView_Activity extends AppCompatActivity {

    ListView listView;
    private int[] pic1 = {R.drawable.a1,R.drawable.a1};
    private String[] textName = {"水星","金星"};
    private List<Map<String,Object>> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_listview2);

        listView = findViewById(R.id.listview1);

        data = new ArrayList<>();
        //将列表项数据封装成List对象
        for (int i = 0 ; i < pic1.length; i++){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("pic",pic1[i]);
            map.put("name",textName[i]);
            //Toast.makeText(listView_Activity.this, map.get("pic").toString(), Toast.LENGTH_SHORT).show();
            data.add(map);
        }


        /*
         * context:上下文
         * data:数据集合,数据集合必须是map类型的
         * resource:列表项布局的资源ID
         * from:数据集合中map的键
         * to:列标布局相对应的ID
         */
//        SimpleAdapter simpleAdapter = new SimpleAdapter(context,data,resource,from,to);
        //创建适配器对象
        SimpleAdapter simpleAdapter = new SimpleAdapter(listView_Activity.this,
                        data,
                        R.layout.layout_listview,//列表项小布局的xml文件
                        new String[]{"pic","name"},
                        new int[]{R.id.pic1,R.id.textname1});


        //绑定适配器
        listView.setAdapter(simpleAdapter);
        //listView的监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Log.i("tag","wxsZcxdsvdscw");
//                Toast.makeText(listView_Activity.this, "wowowowowowoowowowo", Toast.LENGTH_SHORT).show();
                Toast.makeText(listView_Activity.this,
                        "您点击的是"+textName[arg2],Toast.LENGTH_LONG).show();
            }
        });

    }

}

layout_listview2.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="vertical">



    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />





</LinearLayout>

layout_listview.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/pic1"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/textname1"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="6"
        android:gravity="center"
        android:textSize="17sp"
        android:textColor="#ff0000"/>



</LinearLayout>

实现效果:

实现方案2:自定义适配器

listView_Activity:

package com.example.myapplication_one;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class listView_Activity extends AppCompatActivity {

    ListView listView;
    List<Plant> data_2;//xinjia

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_listview2);

        initdata();//xinjia

        listView = findViewById(R.id.listview1);

        //xinjia
        //创建适配器
        PlantAdapter adapter = new PlantAdapter(
                listView_Activity.this,
                R.layout.layout_listview,
                data_2
        );
        //xinjia
        //绑定适配器
        listView.setAdapter(adapter);


        //listView的监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Log.i("tag","wxsZcxdsvdscw");
//                Toast.makeText(listView_Activity.this, "wowowowowowoowowowo", Toast.LENGTH_SHORT).show();
                Toast.makeText(listView_Activity.this,
                        "hahahahhahahahhahahahhah",Toast.LENGTH_LONG).show();
            }
        });

    }

    private void initdata() {
        data_2 = new ArrayList<Plant>();
        Plant plant1 = new Plant(R.drawable.a1,"水星");
        Plant plant2 = new Plant(R.drawable.excel,"金星");
        data_2.add(plant1);
        data_2.add(plant2);
    }


}

Plant.java(Java类):

package com.example.myapplication_one;

public class Plant {
    private int image;//行星图片
    private String name;//行星名字

    public Plant() {
        super();
    }

    public Plant(int image, String name) {
        this.image = image;
        this.name = name;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

PlantAdapter.java(Java类):

package com.example.myapplication_one;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class PlantAdapter extends BaseAdapter {

    private int resourceId;
    private List<Plant> data;
    private Context context;

    //构造方法
    public PlantAdapter(Context context, int resource, List<Plant> data){
        this.context = context;
        this.resourceId = resource;
        this.data = data;
    }

    //获取列表项的个数
    @Override
    public int getCount() {
        return data.size();
    }

    //获取i位置上的列表对象
    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    //获取i位置上的列表对象的id
    @Override
    public long getItemId(int i) {
        return i;
    }

    //获取i位置上的列表视图
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //获取i位置上的数据对象
        Plant plant = data.get(i);
        //加载列表项的布局文件
        view = View.inflate(context,resourceId,null);//拿到页面布局,设置为null就是不使用父类的页面布局效果
        ImageView imageView = view.findViewById(R.id.pic1);
        TextView textView = view.findViewById(R.id.textname1);
        //将数据设置列表项的控件上
        imageView.setImageResource(plant.getImage());
        textView.setText(plant.getName());
        return view;
    }
}

//自定义适配器页面布局的设置

layout_listview2.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="vertical">



    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />





</LinearLayout>

layout_listview.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/pic1"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/textname1"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="6"
        android:gravity="center"
        android:textSize="17sp"
        android:textColor="#ff0000"/>



</LinearLayout>

实现效果:

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值