ListView——SimpleAdapter

SimpleAdapter主要用于图文结合的内容

1.在主布局中添加<ListView>标签

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

2.编写行布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    >
    <ImageView
        android:id="@+id/im_logo"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher_round"
        android:layout_alignParentLeft="true"
        />

    <TextView
        android:id="@+id/text_simple"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:text="实例"
        android:textSize="24sp" />
</RelativeLayout>

行布局效果:

3.编写代码

package com.example.learningandroid;

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 {
    //1.获取ListView对象
    ListView listView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.lv_main);
        //2.准备数据源
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("im_logo",R.drawable.test);//键值对中,key为行布局xml中对应控件id,不是一定要一致,只是为了记忆方便
        map.put("text_simple","实例1");
        list.add(map);
        map = new HashMap<String, Object>();
        map.put("im_logo",R.drawable.test);
        map.put("text_simple","实例2");
        list.add(map);
        //3.准备适配器
        SimpleAdapter adapter = new SimpleAdapter(
                this,//上下文
                list,//数据库
                R.layout.simplelo,//行布局xml名称
                new String[]{"im_logo","text_simple"},//Map中的keys
                new int[]{R.id.im_logo,R.id.text_simple}//行布局中的id,要对应上面的key的顺序
        );
        //4.关联listView
        listView.setAdapter(adapter);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答这个问题。 ListView 是一种常用的 Android 控件,可以用于展示一系列数据。SimpleAdapter 是一种适配器,可以将数据绑定到 ListView 上。 下面是实现列表项的步骤: 1. 准备数据 首先需要准备数据。一般来说,数据可以是一个 List<Map<String, Object>>,其中每个 Map 对应一个列表项,Map 中的键值对表示列表项的属性,比如标题、图片等。例如: ``` List<Map<String, Object>> data = new ArrayList<>(); Map<String, Object> item1 = new HashMap<>(); item1.put("title", "标题1"); item1.put("image", R.drawable.icon1); data.add(item1); Map<String, Object> item2 = new HashMap<>(); item2.put("title", "标题2"); item2.put("image", R.drawable.icon2); data.add(item2); ``` 2. 创建 SimpleAdapter 接下来需要创建 SimpleAdapter,将数据绑定到 ListView 上。SimpleAdapter 的构造函数需要传入以下参数: - Context:上下文对象。 - data:数据源。 - resource:列表项的布局文件。 - from:数据源中 Map 的键名数组,表示要绑定到哪些视图上。 - to:列表项布局文件中视图的 ID 数组,表示要绑定到哪些视图上。 例如: ``` SimpleAdapter adapter = new SimpleAdapter( this, // 上下文对象 data, // 数据源 R.layout.list_item, // 列表项布局文件 new String[] { "title", "image" }, // 数据源中 Map 的键名数组 new int[] { R.id.title, R.id.image } // 列表项布局文件中视图的 ID 数组 ); ``` 3. 设置 Adapter 最后需要将 Adapter 设置到 ListView 上。例如: ``` ListView listView = findViewById(R.id.list_view); listView.setAdapter(adapter); ``` 这样就完成了列表项的实现。当然,还可以对列表项进行一些定制化,比如添加点击事件等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值