本文實(shí)例講述了Android基于BaseExpandableListAdapter實(shí)現(xiàn)的二級列表仿通話記錄功能。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括贊皇網(wǎng)站建設(shè)、贊皇網(wǎng)站制作、贊皇網(wǎng)頁制作以及贊皇網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,贊皇網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到贊皇省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
android SDK中帶有這樣類似的例子,但是那個(gè)還是靜態(tài)數(shù)據(jù),沒有實(shí)際應(yīng)用價(jià)值,參考意義不大。
網(wǎng)上找了很多,還是那樣的情況,幾乎是同一篇文章,大家轉(zhuǎn)來轉(zhuǎn)去。況且,那篇例子也是靜態(tài)的數(shù)據(jù)。
還是自己試試,自己寫一個(gè)吧。程序讀取手機(jī)系統(tǒng)的通話記錄,按聯(lián)系人分組,顯示到列表。
開發(fā)工具:eclipse
運(yùn)行環(huán)境:htc G9 android2.3.3
不多說,先看效果:
展開后的效果:
繼續(xù)展開的效果:
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來實(shí)現(xiàn),具體請看代碼。
主角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); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
分享名稱:Android基于BaseExpandableListAdapter實(shí)現(xiàn)的二級列表仿通話記錄功能詳解
網(wǎng)站鏈接:http://www.rwnh.cn/article14/gcgsge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、靜態(tài)網(wǎng)站、虛擬主機(jī)、響應(yīng)式網(wǎng)站、網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)