前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【鼠】安卓学习杂记(二十四)——Android之Adapter之SimpleAdapter(简单适配器(不常用)——需写简单的布局文件)

【鼠】安卓学习杂记(二十四)——Android之Adapter之SimpleAdapter(简单适配器(不常用)——需写简单的布局文件)

作者头像
訾博ZiBo
发布2025-01-06 13:53:42
发布2025-01-06 13:53:42
4900
代码可运行
举报
运行总次数:0
代码可运行
一、效果图
二、XML代码

自定义布局文件:

代码语言:javascript
代码运行次数:0
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        />


</LinearLayout>

activity_main.xml主布局文件

代码语言:javascript
代码运行次数:0
复制
<?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/lv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

</LinearLayout>
三、Java代码
代码语言:javascript
代码运行次数:0
复制
package com.example.administrator.app_adapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
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 MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化ListView
        ListView lv1 = findViewById(R.id.lv1);
        //创建简单适配器;参数1:上下文;参数2:数据源(特定泛型的集合数据源);参数3:自定义的列表项布局文件;
        //参数4:记录Map(数据源)中的键名;参数5:绑定(自定义布局)视图中的ID
        final SimpleAdapter simpleAdapter = new SimpleAdapter(this,getdata(),
                R.layout.item_layout,new String[]{"image", "text"}, new int[]{R.id.image, R.id.text});
        //为视图绑定适配器
        lv1.setAdapter(simpleAdapter);
        //为每项添加点击事件
        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击事件
                /*parent是指当前的listview;
                 *view是当前listview中的item的view的布局,就是可用这个view获取里面控件id后操作控件
                 * position是当前item在listview中适配器的位置
                 * id是当前item在listview里第几行的位置
                 */
                //获得选中项中的HashMap对象
                HashMap<String,String> map=(HashMap<String,String>)parent.getItemAtPosition(position);
                String Text=map.get("text");
                Toast.makeText(MainActivity.this,Text,Toast.LENGTH_SHORT).show();
            }
        });
    }
    //创建数据源
    private List<Map<String, Object>> getdata() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据1");
        list.add(map);
        map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据2");
        list.add(map);
        map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据3");
        list.add(map);
        map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据4");
        list.add(map);
        map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据5");
        list.add(map);
        map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据6");
        list.add(map);
        map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据7");
        list.add(map);
        map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据8");
        list.add(map);
        map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据9");
        list.add(map);
        map = new HashMap<>();
        map.put("image", R.mipmap.ic_launcher);
        map.put("text", "数据10");
        list.add(map);
        return list;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、效果图
  • 二、XML代码
  • 三、Java代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档