版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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年辽宁省沈阳市成考专升本医学综合自考模拟题(含答案)
- 2026年药房消化系统模拟试题及答案详解
- 2026年网络安全防护培训模拟试卷(含答案)
- 2026年经济学专升本国际金融模拟试卷(含答案)
- 2026年融资融券考试题库(含答案)
- 医务人员职业暴露试题(附答案)
- 公路施工项目安全风险评估报告范本
- 安静的力量主题班会课件
- 《2025型钢采购合同》
- 全麻术后导尿管刺激征管理
- 以例启思:高中思想政治教学中案例教学法的深度应用与策略探索
- 高等职业学校空中乘务专业 实训教学条件建设标准
- 《药事管理与法规》课件-项目六 药品生产质量管理
- 温泉酒店室内装修施工方案
- 有理数加减混合运算测试题卷100道
- (正式版)JB∕T 14734-2024 深冷处理设备热处理技术要求
评论
0/150
提交评论