本文转载自:http://blog.csdn.net/jueblog/article/details/11857281

步骤

使用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. <LinearLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_marginLeft="18dp"
  21. android:layout_marginRight="18dp"
  22. android:layout_marginTop="2dp"
  23. android:layout_marginBottom="2dp"
  24. android:background="@drawable/btn_style_four_normal">
  25. <ImageView
  26. android:id="@+id/imageView1"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:src="@drawable/sm_searchbtn"
  30. android:layout_marginRight="10dp"/>
  31. <EditText
  32. android:id="@+id/editText1"
  33. android:layout_width="match_parent"
  34. android:layout_height="35dp"
  35. android:background="@null"
  36. android:ems="10" >
  37. <requestFocus />
  38. </EditText>
  39. </LinearLayout>
  40. <ListView
  41. android:id="@+id/listView1"
  42. android:layout_width="match_parent"
  43. android:paddingBottom="50dp"
  44. android:cacheColorHint="#00000000"
  45. android:layout_height="match_parent" >
  46. </ListView>
  47. </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. <ImageView
  10. android:id="@+id/img"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_margin="5dp" />
  14. <LinearLayout
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:orientation="vertical" >
  18. <LinearLayout
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:orientation="horizontal" >
  22. <TextView
  23. android:id="@+id/title"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:textColor="#000"
  27. android:textSize="20sp" />
  28. <TextView
  29. android:id="@+id/time"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginLeft="110dp"
  33. android:textColor="#000"
  34. android:textSize="18sp" />
  35. </LinearLayout>
  36. <TextView
  37. android:id="@+id/info"
  38. android:layout_width="wrap_content"
  39. android:layout_height="fill_parent"
  40. android:layout_marginTop="3dp"
  41. android:textColor="#000"
  42. android:textSize="15sp" />
  43. </LinearLayout>
  44. </LinearLayout>

WeixinActivity.java文件

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

效果图

【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

    步骤 使用BaseAdapter实现复杂的ListView的步骤: 1. 数据你要准备好 List getData(). 2. 继承ListActivity专有屏,不再需要setContentView ...

  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. android基础---->NDK的使用

    NDK的发布,使“Java+C”的开发方式终于转正,成为官方支持的开发方式.NDK将是Android平台支持C开发的开端,今天我们开始ndk的学习. NDK的简要说明 ndk是什么: The Nati ...

  2. ubuntu14.04 LTS Visual Studio Code 编辑器推荐

    除了ubuntu geany (茶壶图标) 这个一直爱好的编辑器,发现一个新的编辑器“Visual Studio Code”,也是很好用,记录下 https://code.visualstudio.c ...

  3. 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn 可并堆

    [BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's f ...

  4. 微信小程序 --- page.js文件

    page.js文件是写当前 page.wxml 页面的 JS 脚本文件: 示例: //获取应用实例 const app = getApp() Page({ data: { navComOneOnOff ...

  5. 问答项目---用户注册的那些事儿(PHP验证)

    JS 验证之后,还需要通过PHP验证: 提交过来的名称不一样,可以用字段映射: 在自动验证的时候,如果这个字段被映射,那么自动验证的时候,自动验证的就是 映射过后的字段: 控制器示例: //注册表单处 ...

  6. 二维坐标系极角排序的应用(POJ1696)

    Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3170   Accepted: 2029 Descrip ...

  7. Spring----学习参考博客书单链接

    [References] 1.IOC之基于Java类的配置Bean 2.IOC之基于注解的配置bean(上) 3.Spring之IOC的注入方式总结 4.Spring之IOC自动装配解析 5.Spri ...

  8. javascript飞机大战-----005创建子弹对象2

    子弹销毁 /* 创建子弹:因为子弹不是只创建一个所以要用构造函数 注意一点:子弹发射的位置应该是英雄机的正中央的位置,所以需要传点东西进来 */ function Bullet(l,t){ this. ...

  9. MS-SQL数据库定时自动备份

    在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求管理员每天守到晚上1点去备份数据库.要实现数据库的定 ...

  10. codeforces#512 Div2

    pre过了三题 终测又挂了一题 又掉分了 真的是 太菜了 A-In Search of an Easy Problem 水题 有一个1就是hard #include <bits/stdc++.h ...