一般在开发中我们都需要在listview中添加数据显示在界面上

1.首先我们会在布局中写一个listview

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#EFEFF4"
  6. android:clipChildren="false"
  7. >
  8.  
  9. <ListView
  10. android:id="@+id/id_rada_list"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:layout_marginTop="10dp"
  14. android:divider="#00000000"/>
  15.  
  16. </FrameLayout>

2.初始化listview的布局控件

  1. ListView rada_list = findViewById(R.id.id_rada_list);

3.网络请求数据在listview中加载adapter

  1. rada_list.setAdapter(new MyListAdapter());

4.关键步骤adapter添加数据

  1. //listview 设置适配器
  2.  
  3. class MyListAdapter extends BaseAdapter {
  4.  
  5. private DisplayImageOptions options;
  6.  
  7. public MyListAdapter() {
  8. options = new DisplayImageOptions.Builder()
  9. .showImageOnLoading(R.drawable.logo)//加载中图片的设置
  10. .showImageForEmptyUri(R.drawable.logo)//url为null的情况下
  11. .showImageOnFail(R.drawable.logo)//加载图片失败的时候的设置
  12. .cacheInMemory(true)//是否缓存在内存
  13. .cacheOnDisk(true)//是否缓存在硬盘
  14. .considerExifParams(true)//exif:一张图片的附加信息 位置信息、时间信息、角度
  15. .displayer(new FadeInBitmapDisplayer(500))//设置展示器
  16. .build();
  17. }
  18.  
  19. @Override
  20. public int getCount() {
  21. return redEnvelope.size();
  22. }
  23.  
  24. @Override
  25. public Object getItem(int position) {
  26. return redEnvelope.get(position);
  27. }
  28.  
  29. @Override
  30. public long getItemId(int position) {
  31. return position;
  32. }
  33.  
  34. @Override
  35. public View getView(final int position, View convertView, ViewGroup parent) {
  36. ViewHolder holder = null;
  37. if (convertView == null) {
  38. convertView = View.inflate(getApplication(), R.layout.item_radar, null);
  39. holder = new ViewHolder();
  40. holder.ivImageview = convertView.findViewById(R.id.iv_rade_image);
  41. holder.tvName = convertView.findViewById(R.id.tv_name);
  42. holder.tvMonkey = convertView.findViewById(R.id.tv_money);
  43. holder.tvNuber = convertView.findViewById(R.id.tv_nuber);
  44. holder.btCome = convertView.findViewById(R.id.bt_come);
  45. convertView.setTag(holder);
  46. } else {
  47.  
  48. holder = (ViewHolder) convertView.getTag();
  49.  
  50. }
  51. holder.tvName.setText(redEnvelope.get(position).getNickname() + "");
  52. DecimalFormat format = new DecimalFormat("#.##");
  53. double aAmount = redEnvelope.get(position).getActualAmount();
  54. holder.tvMonkey.setText(format.format(aAmount));
  55. holder.tvNuber.setText(redEnvelope.get(position).getNumber() + "");
  56. String photoUrl = redEnvelope.get(position).getPhotoUrl();
  57.  
  58. // String url = GlobalConstants.URL_PREFIX + news.get(position).listimage;
  59. ImageLoader.getInstance().displayImage(photoUrl, holder.ivImageview, options);
  60. holder.btCome.setOnClickListener(new View.OnClickListener() {
  61.  
  62. @Override
  63. public void onClick(View v) {//点击红包雷达 去抢红包
  64. if(redNum == 0){
  65. DialogBuilder.messageDialog(RadarActivity.this).setMessage("您的抢红包次数已使用完啦!!!").show();
  66. return;
  67. }
  68. nickname = redEnvelope.get(position).getNickname();
  69. photoUrl1 = redEnvelope.get(position).getPhotoUrl();
  70. actualAmount = redEnvelope.get(position).getActualAmount();
  71. id = redEnvelope.get(position).getId();
  72. number = redEnvelope.get(position).getNumber();
  73. pos = position;
  74. toValidate();
  75.  
  76. }
  77. });
  78.  
  79. return convertView;
  80. }
  81. }
  82.  
  83. //非静态内部类会默认拥有外部类的实例对象
  84. static class ViewHolder {
  85. public ImageView ivImageview;
  86. public TextView tvName;
  87. public TextView tvMonkey;
  88. public TextView tvNuber;
  89. public Button btCome;
  90.  
  91. }

6.adtpter布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7.  
  8. android:fitsSystemWindows="true"
  9. android:orientation="vertical">
  10.  
  11. <LinearLayout
  12. android:id="@+id/ll_pay_wx"
  13. android:layout_width="match_parent"
  14. android:layout_height="80dp"
  15. android:layout_marginLeft="16dp"
  16. android:background="#FFFFFF"
  17. android:layout_marginRight="13dp"
  18. android:gravity="center_vertical">
  19.  
  20. <com.netease.nim.avchatkit.common.imageview.CircleImageView
  21. android:id="@+id/iv_rade_image"
  22. android:layout_width="50dp"
  23. android:layout_height="50dp"
  24. android:layout_marginEnd="8dp"
  25. android:layout_marginLeft="8dp"
  26. android:layout_marginRight="8dp"
  27. android:layout_marginStart="8dp"
  28. app:civ_border_color="#fffed470"
  29. app:civ_border_width="2dp"
  30. app:layout_constraintEnd_toEndOf="parent"
  31. app:layout_constraintStart_toStartOf="parent"
  32. app:layout_constraintTop_toTopOf="@+id/constraintLayout"
  33. app:srcCompat="@drawable/logo" />
  34.  
  35. <LinearLayout
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_marginLeft="17dp"
  39. android:orientation="vertical">
  40.  
  41. <TextView
  42. android:id="@+id/tv_name"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_marginBottom="2dp"
  46. android:text="红聊科技
  47. " android:textColor="#000000"
  48. android:textSize="15sp"/>
  49.  
  50. <RelativeLayout
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content">
  53.  
  54. <TextView
  55. android:id="@+id/tv_read_yuer"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:textColor="#666666"
  59. android:textSize="13sp"
  60. android:text="剩余"/>
  61.  
  62. <TextView
  63.  
  64. android:id="@+id/tv_money"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:layout_toRightOf="@+id/tv_read_yuer"
  68. android:text="1000"
  69. android:textSize="13sp"
  70. android:textColor="#ff533c"/>
  71.  
  72. <TextView
  73.  
  74. android:layout_width="wrap_content"
  75. android:layout_height="wrap_content"
  76. android:layout_toRightOf="@+id/tv_money"
  77. android:text="元"
  78. android:textSize="13sp"
  79. android:textColor="#666666"/>
  80.  
  81. <TextView
  82. android:id="@+id/tv_view"
  83. android:layout_width="wrap_content"
  84. android:layout_height="wrap_content"
  85. android:layout_marginLeft="90dp"
  86. android:textSize="13sp"
  87. android:textColor="#666666"
  88. android:text="剩余"/>
  89.  
  90. <TextView
  91. android:id="@+id/tv_nuber"
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content"
  94. android:layout_toRightOf="@+id/tv_view"
  95. android:text="10"
  96. android:textSize="13sp"
  97. android:textColor="#666666"/>
  98.  
  99. <TextView
  100.  
  101. android:layout_width="wrap_content"
  102. android:layout_height="wrap_content"
  103. android:layout_toRightOf="@+id/tv_nuber"
  104. android:text="个红包"
  105. android:textSize="13sp"
  106. android:textColor="#666666"/>
  107.  
  108. </RelativeLayout>
  109.  
  110. <Button
  111. android:id="@+id/bt_come"
  112. android:layout_width="73dp"
  113. android:layout_height="27dp"
  114. android:layout_marginLeft="170dp"
  115. android:layout_marginTop="-22dp"
  116. android:textColor="#504f4f"
  117. style="?android:attr/borderlessButtonStyle"
  118. android:background="@drawable/shape_backgroundd"
  119. android:text="去抢"/>
  120.  
  121. </LinearLayout>
  122.  
  123. </LinearLayout>
  124.  
  125. <View
  126. android:layout_width="match_parent"
  127. android:layout_height="1dip"
  128. android:layout_marginLeft="16dp"
  129. android:layout_marginRight="13dp"
  130. android:background="#E0E0E0"/>
  131.  
  132. </LinearLayout>

网络请求Adapter添加数据的更多相关文章

  1. 微信小程序教学第二章(含视频):小程序中级实战教程之预备篇 - 封装网络请求及 mock 数据

    § 封装网络请求及 mock 数据 本文配套视频地址: https://v.qq.com/x/page/i05544fogcm.html 开始前请把 ch2-3 分支中的 code/ 目录导入微信开发 ...

  2. ios MVVM实践 刷新网络请求+tableView展示数据

    [实现效果] [目录结构相关] 此示例展示用的是MVVM结构形式,表述如下 M:数据Model的存储,可以用来对属性进行处理.(即胖model概念,上图中xx万人订阅这个处理方法写在Model内) V ...

  3. Python 学习之urllib模块---用于发送网络请求,获取数据(2)

    接着上一次的内容. 先说明一下关于split()方法:它通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串(把一个字符串分割成很多字符串组成的list列表) 语法: ...

  4. Python 学习之urllib模块---用于发送网络请求,获取数据(4)

    承接将查询城市编码的结果保存到文件中,以字典的形式保存,目的是为了在查询某个城市的天气的时候,能够通过输入的城市名称,找到对应的城市编码.所以此结果字典的数据结构,就是city={城市名称:城市编码} ...

  5. Python 学习之urllib模块---用于发送网络请求,获取数据(3)

    上节内容,是得到了省/直辖市编码,如web='http://m.weather.com.cn/data5/city01',我们需要继续获取此接口的数据,于是进行下面的操作 for  i  in   b ...

  6. Python 学习之urllib模块---用于发送网络请求,获取数据(5)

    查询城市天气最后一节 需要导入上一节的结果city10.py #!/usr/bin/python# -*- coding: UTF-8 -*-import urllib.requestfrom  ci ...

  7. Python 学习之urllib模块---用于发送网络请求,获取数据

    1.urllib urllib是Python标准库的一部分,包含urllib.request,urllib.error,urllib.parse,urlli.robotparser四个子模块. (1) ...

  8. 阶段一:通过网络请求,获得并解析JSON数据(天气应用)

    “阶段一”是指我第一次系统地学习Android开发.这主要是对我的学习过程作个记录. 在上一篇阶段一:解析JSON中提到,最近在写一个很简单的天气预报应用.即使功能很简单,但我还是想把它做成一个相对完 ...

  9. iOS开发——实战篇Swift篇&UItableView结合网络请求,多线程,数据解析,MVC实战

    UItableView结合网络请求,多线程,数据解析,MVC实战 学了这么久的swift都没有做过什么东西,今天就以自己的一个小小的联系,讲一下,怎么使用swift在实战中应用MVC,并且结合后面的高 ...

随机推荐

  1. github/gitee使用办法

    github/gitee只要添加SSH公钥都是可以连接上的 比如把某个文件上传gitee 首先肯定要有权限    否则会一直提醒failed伤心心 接下来说常用语句 git config --list ...

  2. 一 Struts框架(上)

    Struts2 是基于MVC的WEB框架 经过六年多的发展,Struts1已经成为了一个高度成熟的框架,不管是稳定性还是可靠性都得到了广泛的证明.市场占有率超过20%,拥有丰富的开发人群,几乎已经成为 ...

  3. 让Mustache支持简单的IF语句

    转载:https://blog.csdn.net/iteye_16732/article/details/82070065 Mustache是一种Logic-less templates.不支持if这 ...

  4. ubuntu10.04 32 编译android源码的问题

    ubuntu10.04 32 问题  没有jdk1.6     并且使用apt-get 安装jdk相当麻烦,参照:http://blog.csdn.net/godvmxi/article/detail ...

  5. 【浅色】最强Win7 x64评测

    [浅色]最强Win7 x64评测 [浅色]最强Win7 x86 & x64 | WINOS https://www.winos.me/archives/789.htmlESD671MB,安装后 ...

  6. C++Primer第五版——习题答案详解(五)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...

  7. greendao3.2.0使用

    源代码地址 https://github.com/greenrobot/greenDAO buildscript { repositories { jcenter() mavenCentral() } ...

  8. studio--常见设置

    13.Butterknife插件:zelezny 12.android studio怎么设置打开项目时打开项目列表? 11.stuido   代码背景颜色设置为护眼模式 ======== 13.But ...

  9. minicom 抓取log

    使用minicom也有很长时间了,只用minicom抓过uart log,但是从来没有去保存过这个log,也不知道有这个功能.后来在超级终端中发现有这个功能(传送->捕获文字),想想minico ...

  10. 8、Curator的监听机制

    原生的zookeeper的监听API所实现的方法存在一些缺点,对于开发者来说后续的开发会考虑的细节比较多. Curator所实现的方法希望摒弃原声API 的不足,是开发看起来更加的简单,一些重连等操作 ...