本文实例讲述了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,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",date)); List.add(cv); } } child.add(List); } private voID setAdapter() { listadapter = new Myexpandablelistadapter(CollLogActivity.this); ListVIEw.setAdapter(listadapter); } private voID startquery() { queryHandler.startquery(1,uri,"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程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能详解全部内容,希望文章能够帮你解决Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)