ListView学习

-ListView–
title: ListView学习
tags: android,学习笔记,原创


ListView希望实现的效果

效果图1

分析一下使用的步骤

(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+"行");

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值