【Android基本控件】ListView 的用法

本文介绍如何在Android应用中使用ListView与SimpleAdapter显示书籍列表,并解析点击事件处理过程及Item布局配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SimpleAdapter simpleadapter = new SimpleAdapter(this, bookList, R.layout.mylistitem,new String[]{"bookName","path"}, new int[]{R.id.bookName,R.id.bookPath}); listView.setAdapter(simpleadapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Intent intent = new Intent(); rowItem = bookList.get(position); String string = rowItem.get("path").toString(); intent.putExtra("book",string); intent.setClass(ReaderActivity.this,NovelPage.class); startActivity(intent); }

在这里使用ListView的话,我们需要有一个Adapter来配置 ,下面是ListView的数据属性,这一点和Flex里面的很像。然后通过setOnItemClickListener来设置点击事件的监听函数。

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

这里的第三个参数position就是点击的位置。

下面是Item 的配置 xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/listbk" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="10dip" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="10dip" android:src="@drawable/unknown_cover" /> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/bookName" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="10dip" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="10dip" android:text="TextView" /> <TextView android:id="@+id/bookPath" android:layout_width="210dp" android:layout_height="wrap_content" android:layout_weight="0.36" android:paddingBottom="10dip" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="10dip" android:text="TextView" /> </LinearLayout> </LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值