Android开发之ListView、GridView 详解及示例代码
listview与gridview是android开发中的常用控件,它们和adapter配合使用能够实现很多界面效果。下面分别以实例说明listview、gridview的用法。
1.listview的android开发实例
listview 是android开发中最常用的控件之一,一般构成列表包括三个元素,listview:用来展示列表的视图、adapter:数据与视图连接的桥梁、data:具体的数据包括字符串 、图片或者控件。
适配器一般有以下几种类型:
arrayadapter:android中最简单的一种适配器,专门用于列表控件。只显示一行数据。
simpleadapter:此适配器有最好的扩充性,可以自定义出各种效果。经常使用静态数据填充列表。
cursoradapter:通过游标向列表提供数据。
resourcecursoradapter:这个适配器扩展了cursoradapter,知道如何从资源创建视图。
simplecursoradapter:这个适配器扩展了resourcecursoradapter,从游标中得列创建 textview/imageview视图。下面获取通讯录的示例:
xml/html代码
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg" > <listview android:id="@+id/contacts_list" android:layout_width="fill_parent" android:layout_height="fill_parent" > </listview> </linearlayout>
java代码
package net.csdn.blog.androidtoast; import java.util.arraylist; import java.util.hashmap; import java.util.map; import android.app.activity; import android.database.cursor; import android.os.bundle; import android.provider.contactscontract; import android.view.view; import android.widget.adapterview; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.toast; public class mainactivity extends activity { listview mlistview; arraylist<map<string, string>> listdata; static final string name = "name"; static final string number = "number"; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); getcontacts(); } /** * 获取联系人列表 */ private void getcontacts() { mlistview=(listview) findviewbyid(r.id.contacts_list); listdata = new arraylist<map<string, string>>(); //获取数据库cursor cursor cur=getcontentresolver().query(contactscontract.contacts.content_uri, null, null, null, null); startmanagingcursor(cur); while (cur.movetonext()) { map<string, string> mp = new hashmap<string, string>(); long id = cur.getlong(cur.getcolumnindex("_id")); cursor pcur = getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, null, contactscontract.commondatakinds.phone.contact_id + "=" + long.tostring(id), null, null); // 处理多个号码的情况 string phonenumbers = ""; while (pcur.movetonext()) { string strphonenumber = pcur.getstring(pcur.getcolumnindex( contactscontract.commondatakinds.phone.number)); phonenumbers += strphonenumber + ":"; } phonenumbers += "\n"; pcur.close(); string name = cur.getstring(cur.getcolumnindex("display_name")); mp.put(name, name); mp.put(number, phonenumbers); listdata.add(mp); } cur.close(); // 建立一个适配器去查询数据 listadapter adapter = new simpleadapter(this, listdata, android.r.layout.simple_list_item_2, new string[]{name, number}, new int[] {android.r.id.text1, android.r.id.text2}); mlistview.setadapter(adapter); //为listview添加事件监听 mlistview.setonitemselectedlistener(new listview.onitemselectedlistener(){ @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { toast.maketext(getapplicationcontext(), "当前所在行为:"+long.tostring(parent.getselecteditemid()+1), 1).show(); } @override public void onnothingselected(adapterview<?> parent) { // todo auto-generated method stub } }); } }
2.gridview的android开发实例
gridview 网格视图,用于显示多行多列。直接上示例:
xml/html代码
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg" > <gridview android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numcolumns="3"/> </linearlayout> xml/html代码 <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical"> <imageview android:layout_height="100dip" android:id="@+id/itemimage" android:layout_width="80dip" android:src="@drawable/png1" android:layout_gravity="center_horizontal"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/itemtext" /> </linearlayout>
java代码
package net.csdn.blog.androidtoast; import java.util.arraylist; import java.util.hashmap; import android.app.activity; import android.os.bundle; import android.view.gravity; import android.view.view; import android.widget.adapterview; import android.widget.gridview; import android.widget.simpleadapter; import android.widget.toast; public class mainactivity extends activity { /** called when the activity is first created. */ //定义图片整型数组 private int[] mimages={ r.drawable.png1, r.drawable.png2, r.drawable.png3, r.drawable.png4, r.drawable.png5, r.drawable.png6, r.drawable.png7, r.drawable.png8, r.drawable.png9 }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //实例化gridview gridview mgridview=(gridview) findviewbyid(r.id.gridview); // 生成动态数组,并且传入数据 arraylist<hashmap<string, object>> lstimageitem = new arraylist<hashmap<string, object>>(); for (int i = 0; i < 9; i++) { hashmap<string, object> map = new hashmap<string, object>(); map.put("itemimage", mimages[i]);// 添加图像资源的id map.put("itemtext", "no." + string.valueof(i+1));// 按序号做itemtext lstimageitem.add(map); } //构建一个适配器 simpleadapter simple = new simpleadapter(this, lstimageitem, r.layout.gridviewitem, new string[] { "itemimage", "itemtext" }, new int[] {r.id.itemimage, r.id.itemtext }); mgridview.setadapter(simple); //添加选择项监听事件 mgridview.setonitemclicklistener(new gridview.onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast toast=toast.maketext(getapplicationcontext(), "你选择了"+(position+1)+"号图片", 1); toast.setgravity(gravity.bottom, 0, 0); toast.show(); } }); } }
看了这两个android开发实例,相信大家对listview、gridview的使用有了一定掌握了。大家还可以使用它们和adapter实现更多的功能试试。
以上就是对android listview 和gridview 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
上一篇: 简单谈谈PHP中的trait
下一篇: Android 消息机制问题总结