SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文

参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致

参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id

参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称

参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应

--------------------------------------------------------直接上例子了----------------------------------------------------------------------

activity_fujin_list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:minHeight="@dimen/dp_150"android:paddingTop="@dimen/dp_8"android:background="@drawable/list_item_click_bg_color" ><TextView android:id="@+id/fujin_list_top_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="@dimen/shiliuhao"android:textColor="#000000"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_centerVertical="true"android:layout_marginBottom="@dimen/dp_8"android:layout_marginLeft="@dimen/dp_10"/><TextView android:id="@+id/fujin_list_top_distance"android:layout_height="wrap_content"android:layout_width="wrap_content"android:textColor="#888"android:layout_alignParentRight="true"android:textSize="@dimen/jiuhao"android:layout_marginBottom="@dimen/dp_8"android:layout_marginRight="@dimen/dp_10"android:paddingTop="@dimen/dp_2"/><TextView android:id="@+id/fujin_list_top_fengexian"android:layout_width="fill_parent"android:layout_height="@dimen/dp0_3"android:background="#888"android:layout_below="@+id/fujin_list_top_title"/><ImageView android:id="@+id/fujin_list_img"android:layout_height="@dimen/dp_85"android:layout_width="@dimen/dp_133"android:layout_alignParentLeft="true"android:layout_below="@+id/fujin_list_top_fengexian"android:layout_marginTop="@dimen/dp_13"android:layout_marginLeft="@dimen/dp_7"/><TextView android:id="@+id/fujin_list_text"android:layout_height="wrap_content"android:layout_width="wrap_content"android:textColor="#7e7e7e"android:textSize="@dimen/shisanhao"android:layout_below="@+id/fujin_list_top_fengexian"android:layout_toRightOf="@+id/fujin_list_img"android:layout_marginLeft="@dimen/dp_10"android:layout_marginTop="@dimen/dp_13"android:layout_marginRight="@dimen/dp_10"android:ellipsize="end"android:singleLine="false" android:maxLines="3"android:ems="12"/><TextView android:id="@+id/rmb_logo_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="@dimen/dp_10"android:layout_toRightOf="@+id/fujin_list_img"android:layout_below="@+id/fujin_list_text"android:text="@string/money_rmb_logo"android:textSize="@dimen/shisihao"android:layout_marginTop="@dimen/dp_13"android:layout_alignBottom="@+id/fujin_list_img"/><TextView android:id="@+id/fujin_list_red_money"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_toRightOf="@+id/rmb_logo_1"android:layout_below="@+id/fujin_list_text"android:textColor="@color/red_money_textcolor"android:textSize="@dimen/shisihao"android:layout_marginLeft="@dimen/dp_2"android:layout_marginTop="@dimen/dp_13"android:layout_alignBottom="@+id/fujin_list_img"/><TextView android:id="@+id/rmb_logo_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="@dimen/dp_8"android:layout_toRightOf="@+id/fujin_list_red_money"android:layout_below="@+id/fujin_list_text"android:text="@string/money_rmb_logo"android:textSize="@dimen/shisihao"android:layout_marginTop="@dimen/dp_13"android:layout_alignBottom="@+id/fujin_list_img"/><TextView android:id="@+id/fujin_list_old_money"android:layout_height="wrap_content"android:layout_width="wrap_content"android:textSize="@dimen/shisihao"android:textColor="@color/old_money_textcolor"android:layout_below="@+id/fujin_list_text"android:layout_toRightOf="@+id/rmb_logo_2"android:layout_marginTop="@dimen/dp_13"android:layout_marginLeft="@dimen/dp_2"android:layout_alignBottom="@+id/fujin_list_img"/></RelativeLayout>

activity_fujin.xml(不是全部布局代码,跟listview无关的就不贴了)

<LinearLayoutandroid:id="@+id/fujin_lovelist_ll"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><ListViewandroid:id="@+id/fujin_lovelist_lv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/general_back_white"android:cacheColorHint="#00000000"android:divider="@color/general_back_gary"android:dividerHeight="@dimen/dp_11"android:fastScrollEnabled="true"android:scrollbars="none" ></ListView></LinearLayout>

FujinActivity.java(同样跟listview无关的就不贴了)

private void initGoodsList() {fujin_lovelist_llLayout = (LinearLayout) findViewById(R.id.fujin_lovelist_ll);fujin_lovelist_llLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));lovelv = (ListView) findViewById(R.id.fujin_lovelist_lv); 
     SimpleAdapter lovelistSimpleAdapter = new SimpleAdapter(this, getList(page_index, page_size, count),R.layout.activity_fujin_list_item, new String[] { "bigimage","goodname", "comment", "nowprice", "oldprice" },new int[] { R.id.fujin_list_img, R.id.fujin_list_top_title,R.id.fujin_list_text, R.id.fujin_list_red_money,R.id.fujin_list_old_money });lovelv.setAdapter(lovelistSimpleAdapter);//lovelv.setOnTouchListener(new MyTouchListener());//lovelv.setOnScrollListener(new MyScrollListener());//这两个调用时我做的监听滚动加载的调用,请无视...
//OnItemClickListener获取listview中每个item的监听,并实现传参跳转</span>
     lovelv.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubString goods_index = String.valueOf(arg2);SharedPreferences settings = getSharedPreferences("GoodsXML",Activity.MODE_PRIVATE);SharedPreferences.Editor localEditor = settings.edit();localEditor.putString("goods_index", goods_index);localEditor.commit();Bundle bundle = new Bundle();int PreviousPage = 1;bundle.putInt("PreviousPage", PreviousPage);Intent intent = new Intent(FujinActivity.this,GoodReviewsActivity.class);intent.putExtras(bundle);startActivity(intent);}});}
</span>

效果图:

请尊重原创,转载请注明出处。

如有疑问或意见欢迎评论,有不合理的地方妄勿喷。

Android中动态生成ListView及SimpleAdapter的使用相关推荐

  1. Android中动态更新ListView

    在使用ListView时,会遇到当ListView列表滑动到最底端时,添加新的列表项的问题,本文通过代码演示如何动态的添加新的列表项到ListView中. 实现步骤:调用ListView的setOnS ...

  2. android 动态显示表格,在Android Studio中动态生成并显示表格

    我在Android上创建我的第一个应用程序,因此我有低开发android应用程序的经验,而且我也是第一次使用java.在Android Studio中动态生成并显示表格 我想要什么? 我想从Array ...

  3. Android中动态初始化布局参数以及ConstraintLayout使用中遇到的坑

    Android中动态初始化布局以及ConstraintLayout遇到的一个坑 ConstraintLayout是Android中的一个很强大的布局,它通过控件之间的相对定位,来完成一个layout中 ...

  4. word文档中动态生成excel表格(基金公告系列讲解)

    1.本博文仅为了将之前工作中动态生成XBRL文件需求中word文档动态生成excel部分进行了实现(虽然隔着时间比较久),闲暇之余的考虑,应对大批量文件生成时可采用定时任务+多线程技术+redis队列 ...

  5. R语言使用keras包实现卷积自动编码器模型(Convolutional Autoencoder)、加载keras自带的mnist数据集、训练中动态生成每个epoch后模型训练的loss曲线

    R语言使用keras包实现卷积自动编码器模型(Convolutional Autoencoder).加载keras自带的mnist数据集.训练中动态生成每个epoch后模型训练的loss曲线 目录

  6. android radiogroup 添加,如何在android中动态添加textview到radiogroup

    大家好, 我们想动态地在radiogroup旁边添加textview.基于服务响应,我们需要在不使用xml的情况下将标签添加到radiogroup. 在某些情况下基于最长无线电如何在android中动 ...

  7. 使用POI在Excel中动态生成图表工具类(支持柱状、组合、环状图、折线图、等常用图)

    使用POI在Excel中动态生成图表工具类 使用POI在Excel中动态生成图表工具类 由于公司是一个生成报表的机构,之前一直使用pageOffice,但是公司领导就是不买,你说公司那样有钱磨磨唧唧干 ...

  8. 安卓中动态生成界面布局

    在项目设计和安卓程序开发过程中有时候我们需要试用编程动态生成界面,这样的好处是可以先读取屏幕大小然后动态生成满足屏幕大小的程序,本例的程序最终实现效果如下:例子为一个点菜初界面,左右滑动手指可以实现菜 ...

  9. android中ScrollView嵌套ListView或GridView显示位置问题

    Android中ScrollView中嵌套ListView或GridView时在开始进入界面时总是显示中间位置,开头的位置显示不出来.这种情况下只需要在ScrollView的父控件中添加以下两行代码即 ...

最新文章

  1. 极端值目标值(exterem or outlier target)对应的核心特征的分布差异分析+结合catboost特种重要度(top10)
  2. 结对编程——四则运算过程
  3. sed文本处理常见用法
  4. 方法级权限控制-基于表达式操作
  5. Why AG3 client 815 transport entries disabled
  6. 2-计算机发展及应用
  7. sockjs.js:1609 GET http://192.168.1.119:8080/sockjs-node/info?t=1617976777339 net::ERR_CONNECTION_TI
  8. grunt集成自动启动
  9. PHP+MySql+PDO实现简单增加、删除、修改、查询
  10. axios_的配置对象详细说明---axios工作笔记007
  11. 恢复初始快捷键_CAD常用命令快捷键大全,47个快捷键50个CAD技巧,教你快速画图...
  12. Android Support Annotation Library使用详解
  13. 进入心理死角--程序员不是技术,是心理 +我是菜鸟。
  14. Windows 2003 server下载
  15. C语言中scanf函数的用法
  16. 计算机老是重启进不了桌面,电脑无限重启进不桌面
  17. vscode修改界面颜色及风格(中文英文界面都有)
  18. 常见的几种html转pdf方法
  19. Swift CoreAnimation ---- CALayer的呈现层和模型层
  20. Android 签到打卡日历,自定义日期可带图标(附源码)

热门文章

  1. 于东来:我赚大钱的秘诀在于分掉80%利润
  2. USTCOJ1240 黑屋 位运算
  3. Php Adodb 初探
  4. 自动编码(AE)器的简单实现
  5. hive是什么[博学谷技术支持]
  6. 地理位置查询——elasticsearch
  7. 怎样在64位安卓系统中使用32位SO库 2
  8. [和管子对话] 1 2007-4-5/对面向对象的你言我语
  9. 纯CSS实现淘宝天猫优惠券效果
  10. 写给程序员的Flutter详细教程,实战解析