Android学习笔记-listview实现方式之BaseAdapter
listview是Android开发中最为常用的组件,这里我们就学习一下用BaseAdapter的方式实现listview,
主布局activity_main.xml是这样的:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/ll_root"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ListView
- android:id="@+id/lv"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- </ListView>
- </LinearLayout>
定义一个listitem.xml,这用于在listview组件中进行显示。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="60dip"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/tv_id"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dip"
- android:text="ID"
- android:textColor="#ff0000"
- android:textSize="18sp" />
- <LinearLayout
- android:layout_marginLeft="20dip"
- android:layout_width="fill_parent"
- android:layout_height="60dip"
- android:gravity="center_vertical"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tv_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dip"
- android:text="姓名"
- android:textColor="#000000"
- android:textSize="18sp" />
- <TextView
- android:id="@+id/tv_number"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dip"
- android:text="号码"
- android:textColor="#880000"
- android:textSize="18sp" />
- </LinearLayout>
- </LinearLayout>
那么activiy中的实现逻辑是这样的,由于我是在数据库中进行的取值,所以,定义的有dao类,而读者,完全可以自己定义一个arraylist进行数据的展示,
我们实现的是BaseDdapter,这里呢,这要实现类里面的两个方法,getCount():获取要展示的数据的个数,getView():对数据的展示,这里面有一个重要的方法:inflate(Context context, int resource, ViewGroup root)
context:是上下文,
resource:resource The resource ID to inflate,即要展示在listview中的资源文件的id,
root:root A view group that will be the parent. Used to properly inflate the * layout_* parameters,这个可以设置位空。
- public class MainActivity extends Activity {
- private LinearLayout ll;
- private ListView lv;
- private List<Person> persons;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ll = (LinearLayout) findViewById(R.id.ll_root);
- lv = (ListView) findViewById(R.id.lv);
- //获取数据
- PersonDao dao = new PersonDao(this);
- //persons的类型是List<Person>
- persons = dao.findAll();
- lv.setAdapter(new MyAdapter());
- }
- //默认实现类simplexxx,defaultxxx,basexxx.
- //当集成借口比较多的时候,可以继承base,simple,defaule,这些开头
- private class MyAdapter extends BaseAdapter{
- private static final String TAG = "MyAdapter";
- //控制listview里总共有多少条目
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return persons.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- //得到某个位置的person对象。
- Person person = persons.get(position);
- //把xml加载进来
- View view = View.inflate(MainActivity.this, R.layout.listitem, null);
- //一定要在view对象中寻找孩子的id
- TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
- TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
- TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
- //person.getId(),是int的,+"" ,int一定要转化成String类型的。
- //tv_id.setText(person.getId());会出错。
- tv_id.setText("ID: "+person.getId());
- tv_name.setText("姓名: "+person.getName());
- tv_number.setText("电话: "+person.getNumber());
- return view;
- }
- }
- }
Android学习笔记-listview实现方式之BaseAdapter的更多相关文章
- Android学习笔记——ListView
该工程的功能是实现在一个activity中显示一个列表 以下代码是MainActivity.java中的代码 package com.example.listview; import java.uti ...
- [Android学习笔记]ListView中含有Button导致无法响应onItemClick回调的解决办法
转自:http://www.cnblogs.com/eyu8874521/archive/2012/10/17/2727882.html 问题描述: 当ListView的Item中的控件只是一些展示类 ...
- 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView
目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- 【转】 Pro Android学习笔记(三五):Menu(6):XML方式 & PopUp菜单
目录(?)[-] 利用XML创建菜单 XML的有关属性 onClick事件 Pop-up菜单 利用XML创建菜单 在代码中对每个菜单项进行设置,繁琐且修改不灵活,不能适配多国语言的要求,可以利用资源进 ...
- 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter
目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...
- 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout
目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...
- 【转】 Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner
目录(?)[-] GridView Spinner GridView GridView是网格状布局,如图所示.在了解ListView后,很容易了解GridView.下面是例子的XML文件. <? ...
- 【转】Pro Android学习笔记(十二):了解Intent(下)
解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...
随机推荐
- 2.10.2 section元素
section元素 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...
- 面试之Spring
一.IoC IoC(Inversion of Control):控制反转(是把传统上由程序代码直接操控的对象的生成交给了容器来实现, 通过容器来实现对象组件的装配和管理.所谓"控制反转&qu ...
- python beautifulsoup获取特定html源码
beautifulsoup 获取特定html源码(无需登录页面) import refrom bs4 import BeautifulSoupimport urllib2 url = 'http:// ...
- Spring Boot . 2 -- 用Spring Boot 创建一个Java Web 应用
通过 start.spring.io 创建工程 通过 IDEA 创建工程
- 循环中i++和++i哪个好
推荐使用++i,因为不需要返回临时对象,执行效率更高.
- [Python3网络爬虫开发实战] 7.1-Selenium的使用
Selenium是一个自动化测试工具,利用它可以驱动浏览器执行特定的动作,如点击.下拉等操作,同时还可以获取浏览器当前呈现的页面的源代码,做到可见即可爬.对于一些JavaScript动态渲染的页面来说 ...
- Centos6 安装nginx
一.编译安装nginx 1.安装nginx所需要的库pcre,pcre的全称为:perl compatible regular expression即perl正则表达式,是为了使nginx具备URL重 ...
- 初学数位DP
所谓数位dp,字面意思就是在数位上进行dp,数位的含义:一个数有个位.十位.百位.千位.等等,数的每一位就是数位. 数位DP一般应用于: 求出给定区间[A,B]内,符合条件P[i]的数 i 的个数. ...
- Github ==〉本地(克隆)
[情景] 新员工入职后,一般会将项目下载到本地. [下载(克隆)] 命令 git clone url地址 示例
- 牛客网sql练习
一建表语句 /* Navicat MySQL Data Transfer Source Server : test Source Server Version : 50717 Source Host ...