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个列表项的更多相关文章

  1. 【转】Android 学习笔记——利用JNI技术在Android中调用、调试C++代码

    原文网址:http://cherishlc.iteye.com/blog/1756762 在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在And ...

  2. Android学习笔记————利用JDBC连接服务器数据库

    /******************************************************************************************** * auth ...

  3. android学习笔记12——ListView、ListActivity

    ListView.ListActivity ==> ListView以垂直列表的形式显示所有列表项. 创建ListView的方式: 1.直接使用ListView创建 2.Activity继承Li ...

  4. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  5. 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...

  6. 【转】 Pro Android学习笔记(七八):服务(3):远程服务:AIDL文件

    目录(?)[-] 在AIDL中定义服务接口 根据AIDL文件自动生成接口代码 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.n ...

  7. 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter

    目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...

  8. 【转】Pro Android学习笔记(十二):了解Intent(下)

    解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...

  9. Android学习笔记:Home Screen Widgets(2):关于Widget

    通过widget定义,我们在widget列表中看到了我们的TestWidget.当我们拖拽widget到主页时,假设在appwidet-provider中定义了android:configure的ja ...

随机推荐

  1. Journal of Proteome Research | Prediction of an Upper Limit for the Fraction of Interprotein Cross-Links in Large-Scale In Vivo Cross-Linking Studies (分享人:张宇星)

    题目:Prediction of an Upper Limit for the Fraction of Interprotein Cross-Links in Large-Scale In Vivo ...

  2. javaweb_HTML

    第一章:网页的构成 1.1概念:b/s与c/s 1.1.1 现在的软件开发的整体架构主要分为B/S架构与C/S架构: b/s:浏览器/服务器 c/s:客户端/服务器 客户端:需要安装在系统里,才可使用 ...

  3. 33. CentOS7 静态ip设置

    1.网络连接选择NAT模式: 2.关闭vmware的dhcp:选择编辑-->虚拟网络编辑器,选择VMnet8,去掉使用本地DHCP服务将ip地址分配给虚拟机(D). 3. 点击NAT设置(S)查 ...

  4. cmdb客户端代码完善2

    目录: 1.面试提问 2.完善采集端代码 3.唯一标识的问题 4.API的验证 1.面试会问到的问题: # 1. 为啥要做CMDB?# - 实现运维自动化, 而CMDB是实现运维自动化的基石# - 之 ...

  5. Servlet(四)----HTTP、Response、servletContent

    ##  HTTP协议: 1.请求消息:客户端发送给服务器端的数据 *  数据格式: 1.请求行 2.请求头     3.请求空行 4.请求体 2.响应消息:服务器端发送给客户端的数据 *  数据格式: ...

  6. Contest 157

    2019-10-06 12:15:28 总体感受:总体难度一般,dfs和dp题花了点时间,最后一题dp有思路,但是实现上不够好. 注意点:首先是hard问题的覆盖度依然是很大的问题,其次是要注意审题. ...

  7. JSON字符串带BOM头"ufeff"

    调用三方接口返回值JSON字符串带BOM头"\ufeff",JSON解析死活报错. 我是用SpringBoot的RestTemplate调用三方接口的,一开始返回值我是用对象接收返 ...

  8. OpenCV-Python 轮廓特征 | 二十二

    目标 在本文中,我们将学习 如何找到轮廓的不同特征,例如面积,周长,质心,边界框等. 您将看到大量与轮廓有关的功能. 1. 特征矩 特征矩可以帮助您计算一些特征,例如物体的质心,物体的面积等.请查看特 ...

  9. FME中按照ID值过滤块参照中的多个要素

  10. Sprinboot 整合 RabbitMQ,RabbitMQ 消息重试机制

    当消费者消费消息的时候,出现错误,RabbitMQ 本身会有