一手遮天 Android - view(ListView): ListView 通过 SimpleAdapter 显示数据

一手遮天 Android - view(ListView): ListView 通过 SimpleAdapter 显示数据,第1张

概述项目地址https://github.com/webabcd/AndroidDemo作者webabcd一手遮天Android-view(ListView):ListView通过SimpleAdapter显示数据示例如下:/view/listview/ListViewDemo2.java/***ListView通过SimpleAdapter显示数据**适配器中包含了数据和项模板*/

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 AndroID - vIEw(ListVIEw): ListVIEw 通过 SimpleAdapter 显示数据示例如下:

/vIEw/ListvIEw/ListVIEwDemo2.java

/** * ListVIEw 通过 SimpleAdapter 显示数据 * * 适配器中包含了数据和项模板 */package com.webabcd.androIDdemo.vIEw.ListvIEw;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.Widget.ListVIEw;import androID.Widget.SimpleAdapter;import com.webabcd.androIDdemo.R;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class ListVIEwDemo2 extends AppCompatActivity {    private ListVIEw _ListVIEw1;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_vIEw_ListvIEw_ListvIEwdemo2);        _ListVIEw1 = (ListVIEw) findVIEwByID(R.ID.ListVIEw1);        sample();    }    private voID sample() {        String[] nameList = new String[]{"中国", "美国", "日本"};        String[] commentList = new String[]{"我是中国", "我是美国", "我是日本"};        int[] imgIDList = new int[]{R.drawable.img_sample_son, R.drawable.img_sample_son, R.drawable.img_sample_son};        // List<Map<String, Object>> - 数据源        //   Map<String, Object> - 每一个 item 数据        //     String - 此 item 数据的某一个数据的 key        //     Object - 此 item 数据的某一个数据的 key 对应的 value        List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();        for (int i = 0; i < nameList.length; i++) {            Map<String, Object> item = new HashMap<String, Object>();            item.put("logo", imgIDList[i]);            item.put("name", nameList[i]);            item.put("comment", commentList[i]);            itemList.add(item);        }        // 实例化 SimpleAdapter        SimpleAdapter myAdapter = new SimpleAdapter(this,                itemList, // 数据源                R.layout.item_vIEw_ListvIEw_ListvIEwdemo2, // 自定义项模板(参见 res/layout/item_vIEw_ListvIEw_ListvIEwdemo2.xml)                new String[]{"logo", "name", "comment"}, // 数据的 key                new int[]{R.ID.imglogo, R.ID.txtname, R.ID.txtComment}); // 项模板中的控件 ID(控件的数据会被自动赋值为上面相同位置的 key 对应的 value)        _ListVIEw1.setAdapter(myAdapter);    }}

/layout/activity_vIEw_ListvIEw_ListvIEwdemo2.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="match_parent"/></linearLayout>

/layout/item_vIEw_ListvIEw_ListvIEwdemo2.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:paddingleft="10dp"    androID:orIEntation="vertical">    <ImageVIEw        androID:ID="@+ID/imglogo"        androID:layout_wIDth="64dp"        androID:layout_height="64dp" />    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:orIEntation="vertical">        <TextVIEw            androID:ID="@+ID/txtname"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:textcolor="#1D1D1D"            androID:textSize="24sp" />        <TextVIEw            androID:ID="@+ID/txtComment"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:textcolor="#B4B4B4"            androID:textSize="14sp" />    </linearLayout></linearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

总结

以上是内存溢出为你收集整理的一手遮天 Android - view(ListView): ListView 通过 SimpleAdapter 显示数据全部内容,希望文章能够帮你解决一手遮天 Android - view(ListView): ListView 通过 SimpleAdapter 显示数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/999639.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存