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,关键 ...
随机推荐
- mac 下安装python pil
mac:sudo brew install freetype sudo pip install pillow ubuntu: sudo apt-get install libfreetype6-dev ...
- mysql limit关键字
select * from table_name limit [index, ] length; limit后面跟2个参数: index:索引号,从0开始计算,表示从哪一行开始: length:长度, ...
- 珂朵莉树(Chtholly Tree)学习笔记
珂朵莉树(Chtholly Tree)学习笔记 珂朵莉树原理 其原理在于运用一颗树(set,treap,splay......)其中要求所有元素有序,并且支持基本的操作(删除,添加,查找......) ...
- 笔试算法题(54):快速排序实现之单向扫描、双向扫描(single-direction scanning, bidirectional scanning of Quick Sort)
议题:快速排序实现之一(单向遍历) 分析: 算法原理:主要由两部分组成,一部分是递归部分QuickSort,它将调用partition进行划分,并取得划分元素P,然后分别对P之前的部分和P 之后的部分 ...
- 笔试算法题(37):二叉树的层序遍历 & 最长递增的数字串
出题:要求层序遍历二叉树,从上到下的层次,每一层访问顺序为从左到右,并将节点一次编号,输出如下:如果只要求打印指定的level的节点,应该如何实现. a b c d e f g h i 分 ...
- Git 教程 -- 第一天
什么是Git? Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. 为什么使用Git? 众所周知,版本控制系统分为集中式版本控制系统(SVN.CVS等)与分布式版 ...
- Qt5笔记之数据库(五)SQL表格模型QSqlTableModel
教程网址:http://www.qter.org/portal.php?mod=view&aid=57 0.打开tablemodel.pro文件,加上: QT += coregui sql 注 ...
- ZOJ 3905 Cake
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
- BNUOJ 2105 Distance Queries
Distance Queries Time Limit: 2000ms Memory Limit: 30000KB This problem will be judged on PKU. Origin ...
- BNUOJ 3278 Candies
Candies Time Limit: 1500ms Memory Limit: 131072KB This problem will be judged on PKU. Original ID: 3 ...