步骤

使用BaseAdapter实现复杂的ListView的步骤:

1. 数据你要准备好 List getData()。

2. 继承ListActivity专有屏,不再需要setContentView(xxx)。

3. 创建一个继承自BaseAdapter的类。

4. 为List绑定适配器 setListAdapter(adapter)。

5. 用传统的方式来覆写适配器的getView函数  (从参数convertView里映射布局文件,find各个控件填充数据)。

6. 改写:加入ViewHolder类(定义n个控件的声明) 。  用convertView.setTag(viewHolder)在View和Object之间进行关联.。

7. 给按钮注册点击监听器。可以用Toast或AlertDialogue弹出选择项的数据。

friend_list.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- 这是范例ListView的布局文件,出了ListView,还可以放置其他控件 -->
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#fff"
  7. android:orientation="vertical" >
  8. <TextView
  9. android:id="@+id/textView1"
  10. android:layout_width="match_parent"
  11. android:layout_height="50dp"
  12. android:text="微信"
  13. android:background="#2B3439"
  14. android:gravity="center"
  15. android:textSize="20sp"
  16. android:textColor="#FFFFFF"/>
  17.  
  18. <LinearLayout
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_marginLeft="18dp"
  22. android:layout_marginRight="18dp"
  23. android:layout_marginTop="2dp"
  24. android:layout_marginBottom="2dp"
  25. android:background="@drawable/btn_style_four_normal">
  26. <ImageView
  27. android:id="@+id/imageView1"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:src="@drawable/sm_searchbtn"
  31. android:layout_marginRight="10dp"/>
  32. <EditText
  33. android:id="@+id/editText1"
  34. android:layout_width="match_parent"
  35. android:layout_height="35dp"
  36. android:background="@null"
  37. android:ems="10" >
  38. <requestFocus />
  39. </EditText>
  40. </LinearLayout>
  41.  
  42. <ListView
  43. android:id="@+id/listView1"
  44. android:layout_width="match_parent"
  45. android:paddingBottom="50dp"
  46. android:cacheColorHint="#00000000"
  47. android:layout_height="match_parent" >
  48. </ListView>
  49.  
  50. </LinearLayout>

friend_list_item.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- 这是列表项的布局文件,每一行长什么样子,修改这里 -->
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="80dp"
  6. android:orientation="horizontal"
  7. android:padding="5dip"
  8. android:paddingBottom="15dp" >
  9.  
  10. <ImageView
  11. android:id="@+id/img"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_margin="5dp" />
  15.  
  16. <LinearLayout
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:orientation="vertical" >
  20.  
  21. <LinearLayout
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:orientation="horizontal" >
  25.  
  26. <TextView
  27. android:id="@+id/title"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:textColor="#000"
  31. android:textSize="20sp" />
  32.  
  33. <TextView
  34. android:id="@+id/time"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_marginLeft="110dp"
  38. android:textColor="#000"
  39. android:textSize="18sp" />
  40. </LinearLayout>
  41.  
  42. <TextView
  43. android:id="@+id/info"
  44. android:layout_width="wrap_content"
  45. android:layout_height="fill_parent"
  46. android:layout_marginTop="3dp"
  47. android:textColor="#000"
  48. android:textSize="15sp" />
  49. </LinearLayout>
  50.  
  51. </LinearLayout>

WeixinActivity.java文件

  1. package com.app.weixin;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import com.app.wexin.R;
  8.  
  9. import android.app.Activity;
  10. import android.app.AlertDialog;
  11. import android.content.Context;
  12. import android.content.DialogInterface;
  13. import android.content.Intent;
  14. import android.os.Bundle;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.widget.BaseAdapter;
  19. import android.widget.Button;
  20. import android.widget.ImageView;
  21. import android.widget.ListView;
  22. import android.widget.TextView;
  23.  
  24. public class WeixinActivity extends Activity {
  25. private ImageView img;
  26. private List<HashMap<String, Object>> mData;
  27. private ListView listView;
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.friend_list);
  33. mData = getData();//为刚才的变量赋值
  34. MyAdapter adapter = new MyAdapter(this);//创建一个适配器
  35.  
  36. listView = (ListView) findViewById(R.id.listView1);//实例化ListView
  37. listView.setAdapter(adapter);//为ListView控件绑定适配器
  38. }
  39.  
  40. /** 自定义适配器 */
  41. public class MyAdapter extends BaseAdapter {
  42. private LayoutInflater mInflater;// 动态布局映射
  43.  
  44. public MyAdapter(Context context) {
  45. this.mInflater = LayoutInflater.from(context);
  46. }
  47.  
  48. // 决定ListView有几行可见
  49. @Override
  50. public int getCount() {
  51. return mData.size();// ListView的条目数
  52. }
  53.  
  54. @Override
  55. public Object getItem(int arg0) {
  56. return null;
  57. }
  58.  
  59. @Override
  60. public long getItemId(int arg0) {
  61. return 0;
  62. }
  63.  
  64. @Override
  65. public View getView(int position, View convertView, ViewGroup parent) {
  66. convertView = mInflater.inflate(R.layout.friend_list_item, null);//根据布局文件实例化view
  67. TextView title = (TextView) convertView.findViewById(R.id.title);//找某个控件
  68. title.setText(mData.get(position).get("title").toString());//给该控件设置数据(数据从集合类中来)
  69. TextView time = (TextView) convertView.findViewById(R.id.time);//找某个控件
  70. time.setText(mData.get(position).get("time").toString());//给该控件设置数据(数据从集合类中来)
  71. TextView info = (TextView) convertView.findViewById(R.id.info);
  72. info.setText(mData.get(position).get("info").toString());
  73. img = (ImageView) convertView.findViewById(R.id.img);
  74. img.setBackgroundResource((Integer) mData.get(position).get("img"));
  75. return convertView;
  76. }
  77. }
  78. // 初始化一个List
  79. private List<HashMap<String, Object>> getData() {
  80. // 新建一个集合类,用于存放多条数据
  81. ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
  82. HashMap<String, Object> map = null;
  83. for (int i = 1; i <= 40; i++) {
  84. map = new HashMap<String, Object>();
  85. map.put("title", "人物" + i);
  86. map.put("time", "9月20日");
  87. map.put("info", "我通过了你的好友验证请求");
  88. map.put("img", R.drawable.pic_person);
  89. list.add(map);
  90. }
  91.  
  92. return list;
  93. }
  94. public void showInfo(int position){
  95. getData();
  96. }
  97. }

效果图

【Android】使用BaseAdapter实现复杂的ListView的更多相关文章

  1. 【Android】以BaseAdapter做适配器的ListView及其性能优化

    适配器的Java类 package com.app.adapter; import org.json.JSONArray; import org.json.JSONObject; import and ...

  2. 【转】【Android】使用BaseAdapter实现复杂的ListView

    原文网址:http://blog.csdn.net/jueblog/article/details/11857281 使用BaseAdapter实现复杂的ListView的步骤: 1. 数据你要准备好 ...

  3. 【Android】使用BaseAdapter实现复杂的ListView【转】

    本文转载自:http://blog.csdn.net/jueblog/article/details/11857281 步骤 使用BaseAdapter实现复杂的ListView的步骤: 1. 数据你 ...

  4. Android笔记——BaseAdapter的使用

    Android中的适配器(Adapter)是数据与视图(View)之间的桥梁,用于对要显示的数据进行处理,并通过绑定到组件进行数据的显示. BaseAdapter是Android应用程序中经常用到的基 ...

  5. Android之使用Volley框架在ListView中加载大量图片

    1.listview 中的条目要用 Volley 中的 NetworkImageView,如果直接用ImageView也可以,但是要在getView方法中使用url地址设置为imageView的tag ...

  6. android ArrayAdapter BaseAdapter SimpleAdapter使用讲解

    不是我针对谁,我只想针对新手玩家. 不清楚Adapter作用的可以看一下http://www.cnblogs.com/zhichaobouke/p/5798672.html (括号里的内容都是我主观添 ...

  7. android 应用架构随笔三(ListView)

    import java.util.ArrayList; import java.util.List; import com.heima.googleplay.holder.BaseHolder; im ...

  8. android 项目学习随笔十七(ListView、GridView显示组图)

    ListView.GridView显示组图,处理机制相同 <?xml version="1.0" encoding="utf-8"?> <Li ...

  9. android 项目学习随笔十三(ListView实现ITEM点击事件,将已读状态持久化到本地)

    1.因为给LISTVIEW增加了两个头布局,所以在点击事件ITEM索引会增加2,比如原来第一条数据的索引应该为0,增加两个头布局后,它的索引变为        2,为了使LISTVIEW的ITEM在点 ...

随机推荐

  1. Visio 下载,及密钥

    Visio2010简体中文高级版(永久激活密钥:GR24B-GC2XY-KRXRG-2TRJJ-4X7DC) ed2k://|file|cn_visio_2010_x64_516562.exe|515 ...

  2. Oracle Database 12c Using duplicate standby database from active database Created Active DataGuard

    primary database db_name=zwc, db_unique_name=zwc standby database db_name=zwc, db_unique_name=standb ...

  3. pyqt tabliwdget表头属性修改

    # -*- coding: utf-8 -*-__author__ = 'Administrator'import sysfrom PyQt4 import QtGui class MyWindow( ...

  4. Laravel-路由-控制器

    (慕课网_轻松学会Laravel-基础篇_天秤vs永恒老师) 一.基础路由 二.多请求路由 三.参数路由 四.路由别名 生成url可以使用别名 五.路由群组 六.路由输出视图 七.控制器参数绑定

  5. Android 如何检测一个服务是否还在运行?

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  6. 高性能MySql进化论【转】

    高性能MySql进化论(十二):Mysql中分区表的使用总结 http://binary.duapp.com/category/sql 当数据量非常大时(表的容量到达GB或者是TB),如果仍然采用索引 ...

  7. 【转载】cocos2d-x2.2.3和android的平台环境

    这两天试图按照教程来学习写游戏移植到的横版过关Android在.在网上找了很多教程,但版本号变化.所使用的工具有细微的差别.所以,现在我们还没有准备好,阅读后,下面的文章.最后能够顺利您的手机上跑起来 ...

  8. FLEX中Tree默认展开全部节点

    这里分两种情况,一种是数据源在MXML文件里,如: <mx:XML id="treeXML" format="e4x"> <root> ...

  9. gulp 构建工具

    1. gulp 的简介 gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用她,我们不仅可以 ...

  10. RMAN-configure命令

    在Oracle 10g中的配置情况 使用RMAN>show all; 可以显示出RMAN 配置参数为: CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # ...