版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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天津市滨海新区事业单位招聘25人备考题库必考题
- 2025年有关护理程序试题及答案
- 光疗课件教学课件
- 2026包钢(集团)公司新员工招聘322人考试题库附答案
- 北师大版二上《参加欢乐购物活动》(课件)
- 2025年云南省人民检察院聘用制书记员招聘(22人)笔试考试备考题库及答案解析
- 店长岗位职责与日常管理手册
- 大学生校园创新创业计划书
- 2025-2026学年北师大版五年级数学上册(全册)知识点梳理归纳
- 2021年广东省广州市英语中考试卷(含答案)
- 2025年警考申论真题及答案大全
评论
0/150
提交评论