ListView和BaseAdapter
参考资料:http://www.runoob.com/w3cnote/android-tutorial-listview.html
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.expample.myapplication.MainActivity"> <TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/mainView_id"/> <TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/subView_id"/>
<TextView
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#000000"/> </LinearLayout>
public class MainActivity extends Activity {
ListView lv;
List ls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView_id);
ls = new LinkedList<animals>();
ls.add(new animals("猪说", "我是猪吗?"));
ls.add(new animals("猫说", "我是猫吗?"));
ls.add(new animals("狗说", "我是狗吗?"));
ls.add(new animals("熊说", "我是熊吗?"));
ls.add(new animals("兔说", "我是兔吗?"));
ls.add(new animals("虎说", "我是虎吗?"));
ls.add(new animals("鼠说", "我是鼠吗?"));
ls.add(new animals("鸡说", "我是鸡吗?"));
ls.add(new animals("鱼说", "我是鱼吗?"));
MyAdapter adapter = new MyAdapter((LinkedList<animals>)ls, MainActivity.this);
lv.setAdapter(adapter);
}
class animals{
String main;
String sub;
animals(String main, String sub){
this.main = main;
this.sub = sub;
}
String getMain(){
return main;
}
String getSub(){
return sub;
}
}
class MyAdapter extends BaseAdapter{
LinkedList<animals> mdata;
Context mcontext;
MyAdapter(LinkedList<animals> data, Context context){
this.mdata = data;
this.mcontext = context;
}
@Override
public int getCount() {
return mdata.size();
}
@Override
public Object getItem(int position) {
return mdata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView main;
TextView sub;
LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
convertView = layoutInflater.inflate(R.layout.item_list_animal, null, false);
main = (TextView) convertView.findViewById(R.id.mainView_id);
sub = (TextView)convertView.findViewById(R.id.subView_id);
main.setText(mdata.get(position).getMain());
sub.setText(mdata.get(position).getSub());
return convertView;
}
}
}

ListView和BaseAdapter的更多相关文章
- 第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter
第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter 1.BaseAdapter BaseAdapter是Android应用程序中经常用到的基础数据适配器,它的 ...
- ListView之BaseAdapter
BaseAdapter可以实现自定义的丰富子项视图,本文实现如下所示结果: 实现代码: /* ListView :列表 BaseAdapter 通用的基础适配器 * * */ public class ...
- Android 杂谈---ListView 之BaseAdapter
前言 几种适配器里面相对来说比较简单的一种适配器,在使用时需要实现几个方法,并且也需要对convertView进行优化 此篇文章以使用listView与BaseAdapter来实现表格样式的布局举例( ...
- andorid 列表视图 ListView 之BaseAdapter
.xml <?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android=&qu ...
- Android关于listView的BaseAdapter以及getView的三级优化
1.4个重写方法的含义 自定义Adapter继承自BaseAdapter(通用适配器) getCount(); getItem(); getItemId(); getViewTypaCount() ...
- android 中listview之BaseAdapter的使用
Listview控件不像其他安卓控件那种直接拖拽到界面上就能用,而是采用类似J2EE中的MVC模型的方式使用,需要通过适配器将某种样式的数据或控件添加到其上而使用. MVC模型实现原理是 数据模型M( ...
- android ListView 和 BaseAdapter 应用
步聚: 1.建立ListView对象:--(作用:绑定Adapter呈现数据) 2.建立ListView实现的Item栏位.xml布局:--(作用:实现ListView的栏位布局) 3.建立Item. ...
- ListView之BaseAdapter的使用
话说开发用了各种Adapter之后感觉用的最舒服的还是BaseAdapter,尽管使用起来比其他适配器有些麻烦,但是使用它却能实现很多自己喜欢的列表布局,比如ListView.GridView.Gal ...
- 43.Android之ListView中BaseAdapter学习
实际开发中个人觉得用的比较多是BaseAdapter,尽管使用起来比其他适配器有些麻烦,但是使用它却能实现很多自己喜欢的列表布局,比如ListView.GridView.Gallery.Spinner ...
- ListView配合BaseAdapter
BaseAdapter使用比较麻烦,它是个抽象类,需要重写4个方法分别是getCount() getItem(..) getItemId(..) getVew(..),相应的使用BaseAdapter ...
随机推荐
- 基于Quartz.net的远程任务管理系统 三
在上一篇中,已经把服务端都做好了.那接下来就是Web的管理端了,因为很多时候服务器是有专门的运维来管理的,我们没有权限去操作,所以有个可以管理Job的工具还是很有必要的. Web管理端,我选择现在很成 ...
- you need to be root to perform this command
在linux 终端执行某条命令时 提示一下错误 you need to be root to perform this command 是提示要获取root权限 输入su 回车输入密码 即可解决 参考 ...
- 脚本:定时释放 Linux/CentOS 缓存【转载自:杭州山不高】
定时释放Linux/CentOS缓存的脚本(yl_dropcaches)如下: #!/bin/bash used=`free -m | awk 'NR==2' | awk '{print $3}'` ...
- css 实现关闭按钮 X
.close::before { content: "\2716";} 然后就显示出来了 这里有个更直接的例子 <!DOCTYPE html> <html lan ...
- (三)SSO之CAS框架单点退出,退出到CAS登录界面
应需求的改变.CAS自定义登录页面不安全,不再使用,于是我一下子回到了原点,在linux上部署上了没有加自定义登陆界面的CAS,接下来开始修改CAS自己默认的登录界面为我们的界面. 一下子修改成功是根 ...
- [Objective-C语言教程]复合对象(33)
在Objective-C中,可以在类集群中创建子类,该类集合定义了一个嵌入在其中的类. 这些类对象是复合对象.你可能想知道什么是类集群,下面首先了解什么是类集群. 1. 类集群 类集群是基础框架广泛使 ...
- git 命令摘录
回滚 n 个 commit (增加了revert commit) git revert -n commit_id 回滚到指定的commit_id(不增加commit,回滚的commit_id被删除) ...
- 1.2 Percona XtraDB Cluster Limitations
摘要: 出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该 ...
- WampServer访问出现403forbidden的问题解决
1,软件装上以后出现所有服务运行,80端口未被占用的情况下服务器一直处于离线状态 解决方案如下: 网络上面很多教程多说切换服务器为在线状态即可,但是我发现我的菜单里面并没有,用命令又嫌麻烦 在图表上面 ...
- Python基础部分的疑惑解析(1)
Python介绍: 是一种全能的语言,虽然执行效率低,但是开发效率高 现在也存在多种版本,IPYTHON,JPYTHON,但最重要的是CPYTHON,其他都是作用于各种语言的粘合剂版本,执行效率低,C ...