您的位置:首页 > 移动开发 > Android开发

ListView学习

2015-11-02 23:53 405 查看
-ListView–

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"}


第五个参数类似

其他

点击事件——setOnItemClickListener

listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
setTitle("你点击了第"+ position+"行");

}


});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android listview