ListView、Map、SimpleAdapter组合

转载请注明出处ListView、Map、SimpleAdapter组合_Mr_Leixiansheng的博客-CSDN博客

 

注意:布局时应分清控件优先级、焦点需要汇集在ListView上

(此处CheckBox的点击事件优先级比ListView高,所以需要屏蔽,否则点击无响应)(博主就是在这儿卡了半天)

步骤:

(基本和普通listview操作一样)

1、主布局添加好ListView控件

2、添加ListView需要显示的布局样式xlm文件

3、实例化包含map的数组,加入要显示的内容

4、配置SimpleAdapter

5、ListView添加设置好的适配器

6、设置ListView的监听

代码如下:

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

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

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/content"
            android:text="Text"
            android:textSize="20dp"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/image"
            android:layout_toRightOf="@+id/content"
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/c_box"
            android:focusable="false"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</LinearLayout>
package com.example.leixiansheng.listview1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.Map;

public class MainActivity extends AppCompatActivity {

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ArrayList<Map<String, Object>> arrayList = new ArrayList<>();

        for (int i = 0; i < 100; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("DATA", "data " + (i + 1));
            map.put("PIC", R.mipmap.ic_launcher);
            arrayList.add(map);

        }

        SimpleAdapter adapter = new SimpleAdapter(this, arrayList, R.layout.arts_layout,
                new String[]{"DATA", "PIC"}, new int[]{R.id.content, R.id.image});

        listView = (ListView) findViewById(R.id.list_item);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String str = arrayList.get(i).get("DATA").toString();
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
            }
        });

    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值