版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】androidstudio
Listview简单实例
//layout布局<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="/apk/res/android"
xmlns:tools="/tools"android:layout_width="match_parent"
android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
><ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
/></RelativeLayout>//代码packagecom.example.test.testlistview;importandroid.app.ListActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.ArrayAdapter;importandroid.widget.ListView;importandroid.widget.TextView;importandroid.widget.Toast;importjava.util.ArrayList;importjava.util.List;publicclassListViewArrayadapterextendsListActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_arrayadapter);
List<String>ls=newArrayList<String>();
ls.add("测试数据1");
ls.add("测试数据2");
ls.add("测试数据1");
ls.add("测试数据2");
ls.add("测试数据1");
ls.add("测试数据2");
ls.add("测试数据1");
ls.add("测试数据2");
ls.add("测试数据1");
ls.add("测试数据2");
ls.add("测试数据1");
ls.add("测试数据2");
ArrayAdapteradapter=newArrayAdapter(this,android.R.layout.simple_list_item_1,ls);
setListAdapter(adapter);
//继承ListActivity才有此函数
}
@Override
protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
Strings=((TextView)v).getText().toString();
Toast.makeText(this,"提示"+position+s,Toast.LENGTH_LONG).show();
super.onListItemClick(l,v,position,id);
}}//效果图简单的listview,关键是ArrayAdapter中布局和数据的适配。android.R.layout.simple_list_item_1系统自带的样式,可以根据自己的要求更改布局样式。List<String>ls=newArrayList<String>()用于显示的数据。//SimpleAdapter适配Listview//layout<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"
android:orientation="horizontal"android:layout_width="match_parent"
android:layout_height="match_parent"><ImageView
android:id="@+id/img2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout></LinearLayout>//关键代码设置listview
SimpleAdapteradapter1=newSimpleAdapter(this,lm,R.layout.simple2,newString[]{"title","img","price"},newint[]{R.id.title2,R.id.img2,R.id.price});
setListAdapter(adapter1);//得到数据
publicList<Map<String,Object>>getData2()
{
Map<String,Object>value=newHashMap<String,Object>();
List<Map<String,Object>>lm=newArrayList<Map<String,Object>>();
value.put("title","苹果");
value.put("img",R.drawable.aphone);
value.put("price","价格:5800");
lm.add(value);
value=newHashMap<String,Object>();
value.put("title","三星");
value.put("img",R.drawable.sphone);
value.put("price","价格:4800");
lm.add(value);
value=newHashMap<String,Object>();
value.put("title","华为");
value.put("img",R.drawable.hphone);
value.put("price","价格:6000");
lm.add(value);
return
lm;
}//SimpleCursorAdapter适配Listview用于数据库数据的显示//关键代码
//获得一个指向系统通讯录数据库的Cursor对象获得数据来源
Cursorcursor=getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null);
startManagingCursor(cursor);
//实例化列表适配器
ListAdapterla=newSimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,newString[]{Contacts.People.NAME,Contacts.P
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学主题班会课件:民族团结共筑和谐
- 2026年宁夏固原中小学教师招聘考试题库及答案(教育综合知识完整版)
- 就市场调研数据共享的商洽函(6篇)范文
- 云计算数据中心运营管理与维护指南
- 远离网络沉迷迈向美好未来四年级主题班会课件
- 化学工程与制药工程作业指导书
- 优化项目立项评审流程通知函(3篇范文)
- 感恩父母孝敬长辈家庭美德小学主题班会课件
- 商洽签订长期供货协议函6篇范文
- 项目管理优化提升协作效率方案
- 2026新疆中鑫国贸集团有限公司招聘16人考试参考题库及答案详解
- 2026江苏苏州工业园区部分单位招聘工作人员49人备考题库及参考答案详解一套
- 中南大学2026年强基计划《体育测试+综合面试》试题及答案解析(二)
- 八年级生地会考全真模拟试卷(含参考答案)
- 2026江西日报社(报业传媒集团)社会招聘14人笔试参考试题及答案解析
- 2026年妇产科护理副高模拟真题及答案
- 人教版数学四年级下册期末测试试卷(历年真题)
- 重庆市2026年普通高等学校招生全国统一考试 政治+答案
- 新能源汽车电池回收拆解项目在2025年的技术突破可行性研究报告
- 2025年湖北省从“五方面人员”中选拔乡镇领导班子成员考试历年参考题库含答案详解
- 2023汇川MV21系列低压三相永磁伺服电机技术参数
评论
0/150
提交评论