转自:http://hilary3113.iteye.com/blog/998352

listview加载adapter过程是这样的.

1 先判断adapter 有多少数据项,根据这个数据确定有多少item. 
2 确定每个item里加载哪个View. 
3 把View里加载要显示的数据.

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView android:layout_width="wrap_content"
  6. android:layout_height="wrap_content" android:text="123424312423142134" />
  7. <ListView android:id="@+id/list" android:layout_width="fill_parent"
  8. android:layout_height="wrap_content" />
  9. </LinearLayout>

listView布局文件:item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView android:id="@+id/name" android:layout_width="wrap_content"
  6. android:layout_height="wrap_content" />
  7. <TextView android:id="@+id/sex" android:layout_width="wrap_content"
  8. android:layout_height="wrap_content" />
  9. <TextView android:id="@+id/age" android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" />
  11. </LinearLayout>

定义一个类,用来传值 People.java

  1. public class People {
  2. public String name;
  3. public String sex;
  4. public String age;
  5. }

定义适配器 MyAdapter.java

  1. package com.action;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.TextView;
  10. /**
  11. *@Author: hilary
  12. *@Date: 2011-4-11
  13. **/
  14. public class MyAdapter extends BaseAdapter {
  15. private List<People> list = new ArrayList<People>();
  16. //  private People people = new People();
  17. private Context context;
  18. public MyAdapter(Context context){
  19. this.context = context;
  20. }
  21. //适配器根据getCount()函数来确定要加载多少项
  22. @Override
  23. public int getCount() {
  24. return list.size();
  25. }
  26. @Override
  27. public Object getItem(int paramInt) {
  28. return list.get(paramInt);
  29. }
  30. @Override
  31. public long getItemId(int paramInt) {
  32. return paramInt;
  33. }
  34. /*
  35. * 当列表里的每一项显示到界面时,都会调用这个方法一次,并返回一个view 所以方法里面尽量要简单,不要做没必要的动作(non-Javadoc)
  36. * 我这里为了大家好理解,没有做优化
  37. */
  38. @Override
  39. public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
  40. //得到列表样式的view对象
  41. paramView=LayoutInflater.from(context).inflate(R.layout.item, null);
  42. //通过view来得到Item中的每个控件的操作权
  43. TextView name = (TextView)paramView.findViewById(R.id.name);
  44. TextView sex = (TextView)paramView.findViewById(R.id.sex);
  45. TextView age = (TextView)paramView.findViewById(R.id.age);
  46. //获得list里面的第paramInt个对象,并把值赋给每个控件
  47. People people = list.get(paramInt);
  48. name.setText(people.name);
  49. sex.setText(people.sex);
  50. age.setText(people.age);
  51. //把一项返回,加载这个View
  52. return paramView;
  53. }
  54. public void addList(People people){
  55. list.add(people);
  56. }
  57. }

主类

  1. package com.action;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.ListView;
  5. public class Tab extends Activity {
  6. MyAdapter adapter ;
  7. /** Called when the activity is first created. */
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.main);
  12. System.out.println("*********Tab");
  13. ListView listView =(ListView) findViewById(R.id.list);
  14. adapter = new MyAdapter(this);
  15. setPeople();
  16. listView.setAdapter(adapter);
  17. }
  18. public void setPeople(){
  19. People people;
  20. for(int i=1;i<5;i++){
  21. people = new People();
  22. people.name="张三";
  23. people.sex = "男";
  24. people.age ="22";
  25. adapter.addList(people);
  26. }
  27. }
  28. }

自定义ListView adapter适配器的更多相关文章

  1. 自定义ListView适配器Adapter引用布局文件的情况下实现点击列表项时背景颜色为灰色

    listview控件设置适配器的时候,如果使用自定义的adapter,比如MyArrayAdapter extends ArrayAdapter<String> 如果listitem布局文 ...

  2. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  3. Android中ListView 控件与 Adapter 适配器如何使用?

    一个android应用的成功与否,其界面设计至关重要.为了更好的进行android ui设计,我们常常需要借助一些控件和适配器.今天小编在android培训网站上搜罗了一些有关ListView 控件与 ...

  4. android之ListView,详细介绍实现步骤,举例,自定义listview适配器

    android之ListView,详细介绍实现步骤,举例,自定义listview适配器 本文来源于www.ifyao.com禁止转载!www.ifyao.com android中如何使用listVie ...

  5. android listview使用自定义的adapter没有了OnItemClickListener事件解决办法

    在使用listview的时用使用自定义的adapter的时候,如果你的item布局中包含有Button,Checkable继承来的所有控件,那么你将无法获取listview的onItemClickLi ...

  6. 自定义ListView适配器

    继承BaseAdapter类 覆盖以下4个方法: @Override public int getCount() { return users.size(); } @Override public O ...

  7. Android adapter适配器的学习

    学习Android有一点时间,说说自己的学习感悟. 首先呢,先说说适配器的作用,顾名思义,它就是把数据定义好一定的规则,使得我们可以用到ListView GridView等上面 所以说这玩意,还是得好 ...

  8. Android 自定义ListView

    本文讲实现一个自定义列表的Android程序,程序将实现一个使用自定义的适配器(Adapter)绑定 数据,通过contextView.setTag绑定数据有按钮的ListView. 系统显示列表(L ...

  9. Android之listview && adapter

    今天我们讲的也是非常重要的一个控件listview-最常用也是最难的 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...

随机推荐

  1. before_request after_request

    Flask我们已经学习很多基础知识了,现在有一个问题 我们现在有一个 Flask 程序其中有3个路由和视图函数,如下: from flask import Flask app = Flask(__na ...

  2. python抓取链家房源信息(二)

    试着用scrapy将之前写的抓取链家网信息的重新写了写 然后先是用了第一页的网页作为测试,调试代码,然后发现总是抓取的时候遇见了 类似于这样的问题,并且抓取不到信息 2017-03-28 17:52: ...

  3. 初涉yield

    function* a(i) { console.log('here we go'); yield i; // 必须有*,不然b会作为返回值,而不是执行 yield* b(i); yield i+10 ...

  4. css控制单行文本溢出

    1.溢出属性(容器的) overflow:visible/hidden(隐藏)/scroll/auto(自动)/inherit; visible:默认值,内容不会被修剪,会成现在元素框之外: hidd ...

  5. 495. Teemo Attacking

    In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  6. 找到最大或最小的N个元素---heapq模块

    堆排序heapq的用法 基本用法: 复杂数据结构: # coding=utf- # example.py # Example of using heapq to find the N smallest ...

  7. [实战]MVC5+EF6+MySql企业网盘实战(7)——文件上传

    写在前面 周末了,在家继续折腾网盘,今天实现网盘文件的上传. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) [实战] ...

  8. Java多线程编程——wait()和notify()、notifyAll()

    1.源码 wait() notify() notifyAll()都是Object类中方法.源码如下所示: public final native void notify(); public final ...

  9. Django项目中模板标签及模板的继承与引用【网站中快速布置广告】

    Django项目中模板标签及模板的继承与引用 常见模板标签 {% static %} {% for x in range(x) %}{% endfor %} 循环的序号{% forloop %} 循环 ...

  10. BZOJ1226 SDOI2009学校食堂

    这题状压DP太神了. g[i][j][k]表示前i-1个人都已打到饭,自己和后七个人打饭的情况是j,当前最后一个打饭的与i的关系是k 如果j&1==1说明当前这个人也打了饭,那么可以转移到g[ ...