Android控件之ListView用法实例详解
程序员文章站
2023-11-25 09:41:22
本文实例讲述了android控件之listview用法。分享给大家供大家参考。具体如下:
示例一:
在android开发中listview是比较常用的组件,它以列表的形...
本文实例讲述了android控件之listview用法。分享给大家供大家参考。具体如下:
示例一:
在android开发中listview是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout android:id="@+id/linearlayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mylistview"> </listview> </linearlayout>
my_listitem.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="wrap_content" android:id="@+id/mylistitem" android:paddingbottom="3dip" android:paddingleft="10dip"> <textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/itemtitle" android:textsize="20dip"> </textview> <textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/itemtext"> </textview> </linearlayout>
lsactivity类:
package com.ljq.ls; import java.util.arraylist; import java.util.hashmap; import java.util.list; import android.app.activity; import android.os.bundle; import android.widget.listview; import android.widget.simpleadapter; public class lsactivity extends activity { private listview list = null; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); list = (listview) findviewbyid(r.id.mylistview); //组织数据源 list<hashmap<string, string>> mylist = new arraylist<hashmap<string, string>>(); for(int i=0;i<10;i++) { hashmap<string, string> map = new hashmap<string, string>(); map.put("itemtitle", "this is title"); map.put("itemtext", "this is text"); mylist.add(map); } //配置适配器 simpleadapter adapter = new simpleadapter(this, mylist,//数据源 r.layout.my_listitem,//显示布局 new string[] {"itemtitle", "itemtext"}, //数据源的属性字段 new int[] {r.id.itemtitle,r.id.itemtext}); //布局里的控件id //添加并且显示 list.setadapter(adapter); } }
运行结果如下图所示:
示例二:
目录结构
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <!-- 使用相对布局 --> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <textview android:layout_width="100dip" android:layout_height="wrap_content" android:layout_marginleft="30dip" android:textsize="20dip" android:id="@+id/id"/> <textview android:layout_width="100dip" android:layout_height="wrap_content" android:layout_aligntop="@id/id" android:layout_torightof="@id/id" android:textsize="20dip" android:id="@+id/name"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@id/name" android:layout_torightof="@id/name" android:textsize="20dip" android:id="@+id/age"/> </relativelayout>
实体javabean:
package com.ljq.domain; public class person { private string id; private string name; private string age; public person() { super(); } public person(string id, string name, string age) { super(); this.id = id; this.name = name; this.age = age; } public string getid() { return id; } public void setid(string id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getage() { return age; } public void setage(string age) { this.age = age; } }
自定义适配器personadapter
package com.ljq.ls; import java.util.list; import com.ljq.domain.person; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; /** * listview加载adapter的过程 * * 1、先判断adapter有多少数据项,根据这个数据确定有多少个item * * 2、确定每个item里加载哪个view * * 3、在view里加载要显示的数据 * * @author jiqinlin * */ public class personadapter extends arrayadapter{ private layoutinflater layoutinflater = null; private list<person> persons; public personadapter(context context, int textviewresourceid, list objects) { super(context, textviewresourceid, objects); layoutinflater = layoutinflater.from(context); persons = objects; } /** * 获取adapter里有多少个数据项 */ @override public int getcount() { return persons.size(); } @override public object getitem(int position) { return persons.get(position); } @override public long getitemid(int position) { return position; } /** * 创建显示的数据界面 * * adapter的作用就是listview界面与数据之间的桥梁, * 当列表里的每一项显示到页面时,都会调用adapter的getview方法返回一个view。 * 想过没有? 在我们的列表有1000000项时会是什么样的?是不是会占用极大的系统资源? */ @override public view getview(int position, view convertview, viewgroup parent) { /* // 优化前 viewholder holder = new viewholder(); convertview = layoutinflater.inflate(r.layout.main, null); holder.id = (textview)convertview.findviewbyid(r.id.id); holder.name = (textview)convertview.findviewbyid(r.id.name); holder.age = (textview)convertview.findviewbyid(r.id.age); convertview.settag(holder); holder.id.settext(persons.get(position).getid()); holder.name.settext(persons.get(position).getname()); holder.age.settext(persons.get(position).getage()); return convertview; */ // 优化后 viewholder holder; if(convertview == null){ convertview = layoutinflater.inflate(r.layout.main, null); holder = new viewholder(); holder.id = (textview)convertview.findviewbyid(r.id.id); holder.name = (textview)convertview.findviewbyid(r.id.name); holder.age = (textview)convertview.findviewbyid(r.id.age); convertview.settag(holder); }else{ holder = (viewholder)convertview.gettag(); } holder.id.settext(persons.get(position).getid()); holder.name.settext(persons.get(position).getname()); holder.age.settext(persons.get(position).getage()); return convertview; } /** * 界面上的显示控件 * * @author jiqinlin * */ private static class viewholder{ private textview id; private textview name; private textview age; } }
类lsactivity
package com.ljq.ls; import java.util.arraylist; import android.app.listactivity; import android.os.bundle; import android.view.view; import android.widget.listview; import android.widget.toast; import com.ljq.domain.person; public class lsactivity extends listactivity { private arraylist<person> persons = new arraylist<person>(); private personadapter personadapter = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); initdata(); personadapter =new personadapter(lsactivity.this, r.layout.main, persons); setlistadapter(personadapter); registerforcontextmenu(getlistview()); } @override protected void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); person person = persons.get(position); toast.maketext(lsactivity.this, person.getid()+":"+person.getname() +":"+person.getage(), toast.length_long).show(); return; } private void initdata(){ persons.add(new person("序号", "姓名", "年龄")); persons.add(new person("1", "ljq1", "20")); persons.add(new person("2", "ljq2", "20")); persons.add(new person("3", "ljq3", "20")); persons.add(new person("4", "ljq4", "20")); persons.add(new person("5", "ljq5", "20")); persons.add(new person("6", "ljq6", "20")); persons.add(new person("7", "ljq7", "20")); persons.add(new person("8", "ljq8", "20")); persons.add(new person("9", "ljq9", "20")); } }
运行结果
希望本文所述对大家的android程序设计有所帮助。