自定义ListView adapter适配器
转自:http://hilary3113.iteye.com/blog/998352
listview加载adapter过程是这样的.
1 先判断adapter 有多少数据项,根据这个数据确定有多少item.
2 确定每个item里加载哪个View.
3 把View里加载要显示的数据.
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="123424312423142134" />
- <ListView android:id="@+id/list" android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
listView布局文件:item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:id="@+id/name" android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView android:id="@+id/sex" android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView android:id="@+id/age" android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
定义一个类,用来传值 People.java
- public class People {
- public String name;
- public String sex;
- public String age;
- }
定义适配器 MyAdapter.java
- package com.action;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- /**
- *@Author: hilary
- *@Date: 2011-4-11
- **/
- public class MyAdapter extends BaseAdapter {
- private List<People> list = new ArrayList<People>();
- // private People people = new People();
- private Context context;
- public MyAdapter(Context context){
- this.context = context;
- }
- //适配器根据getCount()函数来确定要加载多少项
- @Override
- public int getCount() {
- return list.size();
- }
- @Override
- public Object getItem(int paramInt) {
- return list.get(paramInt);
- }
- @Override
- public long getItemId(int paramInt) {
- return paramInt;
- }
- /*
- * 当列表里的每一项显示到界面时,都会调用这个方法一次,并返回一个view 所以方法里面尽量要简单,不要做没必要的动作(non-Javadoc)
- * 我这里为了大家好理解,没有做优化
- */
- @Override
- public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
- //得到列表样式的view对象
- paramView=LayoutInflater.from(context).inflate(R.layout.item, null);
- //通过view来得到Item中的每个控件的操作权
- TextView name = (TextView)paramView.findViewById(R.id.name);
- TextView sex = (TextView)paramView.findViewById(R.id.sex);
- TextView age = (TextView)paramView.findViewById(R.id.age);
- //获得list里面的第paramInt个对象,并把值赋给每个控件
- People people = list.get(paramInt);
- name.setText(people.name);
- sex.setText(people.sex);
- age.setText(people.age);
- //把一项返回,加载这个View
- return paramView;
- }
- public void addList(People people){
- list.add(people);
- }
- }
主类
- package com.action;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- public class Tab extends Activity {
- MyAdapter adapter ;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- System.out.println("*********Tab");
- ListView listView =(ListView) findViewById(R.id.list);
- adapter = new MyAdapter(this);
- setPeople();
- listView.setAdapter(adapter);
- }
- public void setPeople(){
- People people;
- for(int i=1;i<5;i++){
- people = new People();
- people.name="张三";
- people.sex = "男";
- people.age ="22";
- adapter.addList(people);
- }
- }
- }
自定义ListView adapter适配器的更多相关文章
- 自定义ListView适配器Adapter引用布局文件的情况下实现点击列表项时背景颜色为灰色
listview控件设置适配器的时候,如果使用自定义的adapter,比如MyArrayAdapter extends ArrayAdapter<String> 如果listitem布局文 ...
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- Android中ListView 控件与 Adapter 适配器如何使用?
一个android应用的成功与否,其界面设计至关重要.为了更好的进行android ui设计,我们常常需要借助一些控件和适配器.今天小编在android培训网站上搜罗了一些有关ListView 控件与 ...
- android之ListView,详细介绍实现步骤,举例,自定义listview适配器
android之ListView,详细介绍实现步骤,举例,自定义listview适配器 本文来源于www.ifyao.com禁止转载!www.ifyao.com android中如何使用listVie ...
- android listview使用自定义的adapter没有了OnItemClickListener事件解决办法
在使用listview的时用使用自定义的adapter的时候,如果你的item布局中包含有Button,Checkable继承来的所有控件,那么你将无法获取listview的onItemClickLi ...
- 自定义ListView适配器
继承BaseAdapter类 覆盖以下4个方法: @Override public int getCount() { return users.size(); } @Override public O ...
- Android adapter适配器的学习
学习Android有一点时间,说说自己的学习感悟. 首先呢,先说说适配器的作用,顾名思义,它就是把数据定义好一定的规则,使得我们可以用到ListView GridView等上面 所以说这玩意,还是得好 ...
- Android 自定义ListView
本文讲实现一个自定义列表的Android程序,程序将实现一个使用自定义的适配器(Adapter)绑定 数据,通过contextView.setTag绑定数据有按钮的ListView. 系统显示列表(L ...
- Android之listview && adapter
今天我们讲的也是非常重要的一个控件listview-最常用也是最难的 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...
随机推荐
- before_request after_request
Flask我们已经学习很多基础知识了,现在有一个问题 我们现在有一个 Flask 程序其中有3个路由和视图函数,如下: from flask import Flask app = Flask(__na ...
- python抓取链家房源信息(二)
试着用scrapy将之前写的抓取链家网信息的重新写了写 然后先是用了第一页的网页作为测试,调试代码,然后发现总是抓取的时候遇见了 类似于这样的问题,并且抓取不到信息 2017-03-28 17:52: ...
- 初涉yield
function* a(i) { console.log('here we go'); yield i; // 必须有*,不然b会作为返回值,而不是执行 yield* b(i); yield i+10 ...
- css控制单行文本溢出
1.溢出属性(容器的) overflow:visible/hidden(隐藏)/scroll/auto(自动)/inherit; visible:默认值,内容不会被修剪,会成现在元素框之外: hidd ...
- 495. Teemo Attacking
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- 找到最大或最小的N个元素---heapq模块
堆排序heapq的用法 基本用法: 复杂数据结构: # coding=utf- # example.py # Example of using heapq to find the N smallest ...
- [实战]MVC5+EF6+MySql企业网盘实战(7)——文件上传
写在前面 周末了,在家继续折腾网盘,今天实现网盘文件的上传. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) [实战] ...
- Java多线程编程——wait()和notify()、notifyAll()
1.源码 wait() notify() notifyAll()都是Object类中方法.源码如下所示: public final native void notify(); public final ...
- Django项目中模板标签及模板的继承与引用【网站中快速布置广告】
Django项目中模板标签及模板的继承与引用 常见模板标签 {% static %} {% for x in range(x) %}{% endfor %} 循环的序号{% forloop %} 循环 ...
- BZOJ1226 SDOI2009学校食堂
这题状压DP太神了. g[i][j][k]表示前i-1个人都已打到饭,自己和后七个人打饭的情况是j,当前最后一个打饭的与i的关系是k 如果j&1==1说明当前这个人也打了饭,那么可以转移到g[ ...