android学习笔记——利用BaseAdapter生成40个列表项
RT;
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="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
MyActivity.java
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
ListView myList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList=(ListView)findViewById(R.id.myList);
BaseAdapter adapter =new BaseAdapter() {
@Override
public int getCount() {
//指定一共包含40个选项
return 40;
}
@Override
public Object getItem(int i) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
//重写该方法,该方法的返回值将作为列表项的ID
@Override
public long getItemId(int i) {
return i; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//创建一个LinerarLayout,并向其中添加两个组件
LinearLayout line=new LinearLayout(MyActivity.this);
line.setOrientation(0);
ImageView image=new ImageView(MyActivity.this);
image.setImageResource(R.drawable.ic_launcher);
TextView text=new TextView(MyActivity.this);
text.setText("第"+(i+1)+"个列表项");
text.setTextSize(20);
text.setTextColor(Color.RED);
line.addView(image);
line.addView(text);
//返回LinearLayout实例
return line;
}
};
myList.setAdapter(adapter);
}
}
效果如图:

创建一个BadeAdapter对象,扩展该对象需要重写如下4个方法
* getCount():该方法的返回值控制该Adapter将会包含多少个列表项。
* getItem():该方法的返回值决定第postion处的列表项的内容。
* getItemId(int i):该方法的返回值决定第postion处的列表项的ID。
* getView(int i,View view,ViewGroup viewGroup): 该方法的返回值决定第i处的列表项组件
android学习笔记——利用BaseAdapter生成40个列表项的更多相关文章
- 【转】Android 学习笔记——利用JNI技术在Android中调用、调试C++代码
原文网址:http://cherishlc.iteye.com/blog/1756762 在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在And ...
- Android学习笔记————利用JDBC连接服务器数据库
/******************************************************************************************** * auth ...
- android学习笔记12——ListView、ListActivity
ListView.ListActivity ==> ListView以垂直列表的形式显示所有列表项. 创建ListView的方式: 1.直接使用ListView创建 2.Activity继承Li ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...
- 【转】 Pro Android学习笔记(七八):服务(3):远程服务:AIDL文件
目录(?)[-] 在AIDL中定义服务接口 根据AIDL文件自动生成接口代码 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.n ...
- 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter
目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...
- 【转】Pro Android学习笔记(十二):了解Intent(下)
解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...
- Android学习笔记:Home Screen Widgets(2):关于Widget
通过widget定义,我们在widget列表中看到了我们的TestWidget.当我们拖拽widget到主页时,假设在appwidet-provider中定义了android:configure的ja ...
随机推荐
- SpringMVC框架——文件的上传与下载
使用SpringMVC框架做个小练习,需求: 1.单个图片上传并显示到页面中: 2.多个图片上传并显示到页面中: 3.上传文件后下载文件: 1.pom.xml中添加依赖 <!-- 文件上传 -- ...
- iOS开发:Swift/Objective-C高效生成随机字符串
原文连接 Objective-C版 // 随机生成字符串(由大小写字母.数字组成) + (NSString *)random: (int)len { char ch[len]; for (int in ...
- 第十八周java实验作业
实验十八 总复习 实验时间 2018-12-30 1.实验目的与要求 (1) 综合掌握java基本程序结构: (2) 综合掌握java面向对象程序设计特点: (3) 综合掌握java GUI 程序设 ...
- Servlet(四)----Request
## Request 1.request对象和response对象的原理 1.request和response对象是由服务器创建的.我们来使用他们. 2.request对象是来获取请求消息,resp ...
- 金融和IT的区别
在进入金融圈之前, 我写了十五年的代码, 在San Francisco Bay Area(也就是中国人所说的硅谷)工作过两三年. 去年因为Fintech和香港.NET俱乐部的缘故, 我接触了私人银行和 ...
- [线段树]Codeforces 339D Xenia and Bit Operations
Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- PyTorch ImageNet 基于预训练六大常用图片分类模型的实战
微调 Torchvision 模型 在本教程中,我们将深入探讨如何对 torchvision 模型进行微调和特征提取,所有这些模型都已经预先在1000类的Imagenet数据集上训练完成.本教程将深入 ...
- 图像配准:从SIFT到深度学习
图像配准(Image Registration)是计算机视觉中的基本步骤.在本文中,我们首先介绍基于OpenCV的方法,然后介绍深度学习的方法. 什么是图像配准 图像配准就是找到一幅图像像素到另一 ...
- 面试指南」JS 模块化、组件化、工程化相关的 15 道面试题
JS 模块化.组件化.工程化相关的 15 道面试题 1.什么是模块化? 2.简述模块化的发展历程? 3.AMD.CMD.CommonJS 与 ES6 模块化的区别? 4.它们是如何使用的? 5.exp ...
- Vertica的这些事(十一)——-Vertica备份元数据信息
---备份资源池 SELECT 'CREATE RESOURCE POOL ' || name || CASE WHEN memorysize IS NULL THEN ' ' ELSE ' MEMO ...