`
android_mylove
  • 浏览: 377611 次
社区版块
存档分类
最新评论

Android学习笔记(10)————Android的Listview详解1(ArrayAdapter方式)

 
阅读更多

/********************************************************************************************
* author:conowen@大钟
* E-mail:conowen@hotmail.com
* http://blog.csdn.net/conowen
* 注:本文为原创,仅作为学习交流使用,转载请标明作者及出处。
********************************************************************************************/


SimpleCursorAdapter 方式请看http://blog.csdn.net/conowen/article/details/7306545


1、Listview简述

A view that shows items in a vertically scrolling list. The items come from theListAdapter associated with this view.

简单来说就是,创建Listview,然后给数值就行了。

而这些数值来源有三种方式:ArrayAdapter、SimpleAdapter、SimpleCursorAdapter


第一种是最简单的一种Adapter,是字符串数值,只能在ListView显示出文本信息。

第二种是一种自定义的数据来源,要自定义布局方式,可以放置图片,按钮,文本之类的。

第三种数据来源于数据库。


本文为第一种方式,ArrayAdapter,其他两项的方法都差不多,主要是adapter不同。


2、使用ListView步骤

首先创建Listview组件,然后调用Listview.ArrayAdapter()方法,设置Adapter。


通过调用setOnItemClickListener()接口方法,设置“点击”listview某一项的监听事件。

通过调用setOnItemLongClickListener()接口方法,设置“长按”listview某一项的监听事件。


需要说明的是,当设置匿名内部类new OnItemClickListener()时,eclipse不会自动载入复写函数,要点击左边的错误提示,然后Add unimplemented methods,才能载入复写函数onItemClick()。



关于onItemClick方法,下面详细说说。(以下是官方的说明,参数命名不同而已,类型都是一样的。arg0=parent,arg1=view,arg2=position,arg3=id)

public abstract void onItemClick(AdapterView<?> parent,View view, int position, long id)

Callback method to be invoked when an item in this AdapterView has been clicked.

Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

//当点击listview某一项时,这个回调方法就会被调用。

Parameters
parent view position id
The AdapterView where the click happened.
The view within the AdapterView that was clicked (this will be a view provided by the adapter)
The position of the view in the adapter.
The row id of the item that was clicked.


主要说说最后三个参数,

view——————是你点击的Listview的某一项的内容,来源于adapter。如用((TextView)arg1).getText(),可以取出点击的这一项的内容,转为string类型。

position————是adapter的某一项,如点击了listview第2项,而第2项对应的是adapter的第2个数值,那此时position的值就为1了。

如对应adapter的第3个数值,那此时position的值就为2了。

id———————id的值为点击了Listview的哪一项对应的数值,点击了listview第2项,那id就等于1。

注:这些数值都是从0开始的。





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics