ListView学习
2015-11-02 23:53
405 查看
-ListView–
title: ListView学习
tags: android,学习笔记,原创
ListView希望实现的效果
(2)通过实现ArrayAdapter的构造函数来创建一个ArrayAdapter的对象。
(3)通过ListView的setAdapter()方法绑定ArrayAdapter。
其中 第一个参数是Context context 是上下文
第二个参数 List
一个TextView 显示标题 Title
一个TextView 显示内容 Text
一个ImageView 显示图片
代码如下
第五个参数类似
});
title: ListView学习
tags: android,学习笔记,原创
ListView希望实现的效果
分析一下使用的步骤
(1)定义一个数组来存放ListView中item的内容。(2)通过实现ArrayAdapter的构造函数来创建一个ArrayAdapter的对象。
(3)通过ListView的setAdapter()方法绑定ArrayAdapter。
全文的核心代码是围绕着下面这条语句而写的
:SimpleAdapter simpleAdapter = new SimpleAdapter(this,data,R.layout.item,new String[]{"Image","Title","Text"}, new int[]{R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}); listView.setAdapter(simpleAdapter);
其中 第一个参数是Context context 是上下文
第二个参数 List
各参数相关的核心代码
第一个参数
Context context 是上下文,至于什么是上下文,我也不是很清楚。先挖个坑,以后再填。第二个参数
这样的写法对于我来说并不熟悉,所以需要重视ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>(); for (int i = 0; i < 10; i++) { HashMap<String, Object> hashMap = new HashMap<String, Object>(); hashMap.put("Image", R.drawable.ic_launcher); hashMap.put("Title", "第"+i+"行"); hashMap.put("Text", "这是"+i+"行"); data.add(hashMap); }
第三个参数int resource , 此处是 R.layout.item
从效果来看,每个ListView需要一个TextView 显示标题 Title
一个TextView 显示内容 Text
一个ImageView 显示图片
代码如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/ItemImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" /> <TextView android:id="@+id/ItemTitle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/ItemText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/ItemTitle" />
第四个参数是String[] from
new String[]{"Image","Title","Text"}
第五个参数类似
其他
点击事件——setOnItemClickListenerlistView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { setTitle("你点击了第"+ position+"行"); }
});
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories