Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能详解
程序员文章站
2023-08-25 13:48:15
本文实例讲述了android基于baseexpandablelistadapter实现的二级列表仿通话记录功能。分享给大家供大家参考,具体如下:
android sdk中...
本文实例讲述了android基于baseexpandablelistadapter实现的二级列表仿通话记录功能。分享给大家供大家参考,具体如下:
android sdk中带有这样类似的例子,但是那个还是静态数据,没有实际应用价值,参考意义不大。
网上找了很多,还是那样的情况,几乎是同一篇文章,大家转来转去。况且,那篇例子也是静态的数据。
还是自己试试,自己写一个吧。程序读取手机系统的通话记录,按联系人分组,显示到列表。
开发工具:eclipse
运行环境:htc g9 android2.3.3
不多说,先看效果:
展开后的效果:
继续展开的效果:
main.xml:
<?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" > <expandablelistview android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/list" android:groupindicator="@drawable/tubiao_button" android:layout_gravity="right" android:indicatorright="0px" /> </linearlayout>
lis_item.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingleft="2.0dip" > <imageview android:id="@+id/contact" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/contact" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_marginright="10.0dip" /> <imageview android:id="@+id/open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/min" android:layout_torightof="@id/contact" android:layout_centervertical="true" android:layout_marginleft="5.0dip" android:layout_marginright="5.0dip" /> <textview android:id="@+id/name" android:textappearance="?android:textappearancelarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10.0dip" android:layout_margintop="5.0dip" android:layout_marginright="10.0dip" android:singleline="true" android:layout_torightof="@id/open" android:layout_aligntop="@id/open" /> <imageview android:id="@+id/type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10.0dip" android:layout_marginright="8.0dip" android:layout_marginleft="5.0dip" android:src="@drawable/call_in" android:layout_alignparentright="true" /> <textview android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10.0dip" android:layout_marginright="8.0dip" android:layout_marginleft="10.0dip" android:layout_alignparentright="true" /> <textview android:id="@+id/number" android:textappearance="?android:textappearancesmall" android:ellipsize="marquee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleline="true" android:layout_below="@id/name" android:layout_alignleft="@id/name" android:layout_alignwithparentifmissing="true" /> <textview android:id="@+id/date" android:textappearance="?android:textappearancesmall" android:ellipsize="marquee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleline="true" android:layout_alignparentbottom="true" android:layout_alignparentright="true" /> </relativelayout>
这里的也是采用继承baseexpandablelistadapter来实现,具体请看代码。
主角activity:
public class colllogactivity extends activity { private expandablelistview listview; private myasyncqueryhandler queryhandler; private myexpandablelistadapter listadapter; private arraylist<contentvalues> group; private arraylist<list<contentvalues>> child; private static final uri uri = uri.parse("content://call_log/calls"); private static final string[] projection = { "_id", "number", "date", "type", "new", "name" }; private static final int incoming_type = 1; private static final int outgoing_type = 2; private static final int missed_type = 3; private string currentnumber = "-1"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); listview = (expandablelistview) findviewbyid(r.id.list); queryhandler = new myasyncqueryhandler(getcontentresolver()); group = new arraylist<contentvalues>(); child = new arraylist<list<contentvalues>>(); } @override protected void onresume() { super.onresume(); startquery(); } private class myexpandablelistadapter extends baseexpandablelistadapter { private layoutinflater inflater; public myexpandablelistadapter(context context) { this.inflater = layoutinflater.from(context); } @override public object getchild(int groupposition, int childposition) { return child.get(groupposition).get(childposition); } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = inflater.inflate(r.layout.list_item, null); holder = new viewholder(); holder.name = (textview) convertview.findviewbyid(r.id.name); holder.number = (textview) convertview .findviewbyid(r.id.number); holder.date = (textview) convertview.findviewbyid(r.id.date); holder.count = (textview) convertview.findviewbyid(r.id.count); holder.type = (imageview) convertview.findviewbyid(r.id.type); holder.open = (imageview) convertview.findviewbyid(r.id.open); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } convertview.setbackgroundcolor(color.rgb(54, 54, 54)); contentvalues cv = child.get(groupposition).get(childposition); string name = cv.getasstring("name"); string number = cv.getasstring("number"); string date = cv.getasstring("date"); int type = cv.getasinteger("type"); holder.name.settext(name); holder.number.settext(number); holder.date.settext(date); holder.count.setvisibility(view.gone); settypeimg(holder.type, type); holder.open.setvisibility(view.gone); return convertview; } @override public int getchildrencount(int groupposition) { return child.get(groupposition).size(); } @override public object getgroup(int groupposition) { return group.get(groupposition); } @override public int getgroupcount() { return group.size(); } @override public long getgroupid(int groupposition) { return groupposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = inflater.inflate(r.layout.list_item, null); holder = new viewholder(); holder.name = (textview) convertview.findviewbyid(r.id.name); holder.number = (textview) convertview .findviewbyid(r.id.number); holder.date = (textview) convertview.findviewbyid(r.id.date); holder.count = (textview) convertview.findviewbyid(r.id.count); holder.type = (imageview) convertview.findviewbyid(r.id.type); holder.open = (imageview) convertview.findviewbyid(r.id.open); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } contentvalues cv = (contentvalues) getgroup(groupposition); string name = cv.getasstring("name"); string number = cv.getasstring("number"); string date = cv.getasstring("date"); holder.name.settext(name); holder.number.settext(number); holder.date.settext(date); holder.type.setvisibility(view.gone); holder.count.settext("(" + getchildrencount(groupposition) + ")"); if (isexpanded) { holder.open.setimageresource(r.drawable.min); } else { holder.open.setimageresource(r.drawable.max); } return convertview; } @override public boolean hasstableids() { return false; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } } private class viewholder { textview name; textview number; textview date; textview count; imageview type; imageview open; } private void settypeimg(imageview imageview, int type) { switch (type) { case incoming_type: imageview.setimageresource(r.drawable.call_in); break; case outgoing_type: imageview.setimageresource(r.drawable.call_out); break; case missed_type: imageview.setimageresource(r.drawable.call_miss); break; } } private class myasyncqueryhandler extends asyncqueryhandler { public myasyncqueryhandler(contentresolver cr) { super(cr); } @override protected void onquerycomplete(int token, object cookie, cursor cursor) { if (cursor != null && cursor.getcount() > 0) { cursor.movetofirst(); for (int i = 0; i < cursor.getcount(); i++) { cursor.movetoposition(i); string name = cursor.getstring(cursor .getcolumnindex("name")); string number = cursor.getstring(cursor .getcolumnindex("number")); int type = cursor.getint(cursor.getcolumnindex("type")); long date = cursor.getlong(cursor.getcolumnindex("date")); contentvalues cv = new contentvalues(); cv.put("name", name); cv.put("number", number); cv.put("type", type); cv.put("date", formattimestampstring(colllogactivity.this, date)); addgroupitem(cv); } } if (group.size() > 0) { setadapter(); } } } private void addgroupitem(contentvalues cv) { string number = cv.getasstring("number"); if (!currentnumber.equals(number)) { group.add(cv); addchilditem(number); currentnumber = number; } } private void addchilditem(string number) { arraylist<contentvalues> list = new arraylist<contentvalues>(); cursor cursor = getcontentresolver().query(uri, projection, "number=" + number, null, null); if (cursor != null && cursor.getcount() > 0) { cursor.movetofirst(); for (int i = 0; i < cursor.getcount(); i++) { cursor.movetoposition(i); string name = cursor.getstring(cursor.getcolumnindex("name")); int type = cursor.getint(cursor.getcolumnindex("type")); long date = cursor.getlong(cursor.getcolumnindex("date")); contentvalues cv = new contentvalues(); cv.put("name", name); cv.put("number", number); cv.put("type", type); cv.put("date", formattimestampstring(colllogactivity.this, date)); list.add(cv); } } child.add(list); } private void setadapter() { listadapter = new myexpandablelistadapter(colllogactivity.this); listview.setadapter(listadapter); } private void startquery() { queryhandler.startquery(1, null, uri, projection, null, null, "date desc"); } // 处理日期方法 private static string formattimestampstring(context context, long when) { time then = new time(); then.set(when); time now = new time(); now.settonow(); int format_flags = dateutils.format_no_noon_midnight | dateutils.format_abbrev_all | dateutils.format_cap_ampm; if (then.year != now.year) { format_flags |= dateutils.format_show_year | dateutils.format_show_date; } else if (then.yearday != now.yearday) { format_flags |= dateutils.format_show_date; } else { format_flags |= dateutils.format_show_time; } return dateutils.formatdatetime(context, when, format_flags); } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android布局layout技巧总结》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android操作json格式数据技巧总结》、《android资源操作技巧汇总》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
上一篇: php依赖注入知识点详解