在android通话列表中,如果单纯的显示出来,会将很多同名通话记录也展示出来。像这种情况我们一般是将同名的通话记录group起来;既确保了速度也美观人性化。

要实现这个功能很简单,默认的android Cursor.query方法没有group这个参数;我们只能在原有基础上进行修改。

如下:cursor = cr.query(CallLog.Calls.CONTENT_URI,
new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME,CallLog.Calls.TYPE,CallLog.Calls.DATE},
"1=1) group by "+CallLog.Calls.NUMBER+" order by " +CallLog.Calls.DATE+ " DESC"+" -- (",
null,
CallLog.Calls.DATE);

在这里采用了一个投机取巧的方法,不过功能可以实现。

具体不多说了,直接粘代码:

AppCallMessage.java

package com.zhuoyue.InternetPhoneNote.activity;


import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;


public class AppCallMessage extends Activity {
/** Called when the activity is first created. */

private ListView listview;
private static final int INCOMING_TYPE = 1;
private static final int OUTGOING_TYPE = 2;
private static final int MISSED_TYPE = 3;
private static Cursor cursor;
private static HashMap<String, Object> map;
private static String number;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_callmessage);

listview = (ListView)findViewById(R.id.list);

SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.list_callmessage_item,
new String[]{"name","count","number","numfrom","date","imgtype"},
new int[]{R.id.txt_name,R.id.txt_count,R.id.txt_number,R.id.txt_numfrom,R.id.txt_date,R.id.img_type});
listview.setAdapter(adapter);


listview.setOnItemClickListener(new OnItemClickListener()
{

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
HashMap<String, Object> temp = (HashMap<String, Object>) listview.getItemAtPosition(arg2);
number = (String) temp.get("number");
String name = (String)temp.get("name");
Log.i("hello", number);
if( name.equals(null) )
{
name = "No Name";
}
Intent intent = new Intent();
intent.setClass(AppCallMessage.this, callMessageDetail.class);
intent.putExtra("number", number);
intent.putExtra("name", name);
startActivity(intent);

}


});


}


private ArrayList<HashMap<String,Object>> getData()
{

ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
ContentResolver cr = getContentResolver();
cursor = cr.query(CallLog.Calls.CONTENT_URI,
new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME,CallLog.Calls.TYPE,CallLog.Calls.DATE},
"1=1) group by "+CallLog.Calls.NUMBER+" order by " +CallLog.Calls.DATE+ " DESC"+" -- (" ,
null,
CallLog.Calls.DATE );

for( int i= 0; i<cursor.getCount();i++ )
{
cursor.moveToPosition(i);
map = new HashMap<String, Object>();

if(cursor.getString(cursor.getColumnIndex("name")) == null)
{
map.put("name","No Name" );
map.put("number",cursor.getString(cursor.getColumnIndex("number")));
}
else
{

map.put("name", cursor.getString(cursor.getColumnIndex("name")));
map.put("number",cursor.getString(cursor.getColumnIndex("number")) );
}

int type = cursor.getInt(cursor.getColumnIndex("type"));
map.put("imgtype", setTypeImg(type));
long date = cursor.getLong(cursor.getColumnIndex("date"));
map.put("date",formatTimeStampString(AppCallMessage.this, date));
listItem.add(map);

}
cursor.close();


return listItem;
}

private int setTypeImg( int type) {
int str = R.drawable.call_in;
switch (type) {
case INCOMING_TYPE:
str = R.drawable.call_in;
break;
case OUTGOING_TYPE:
str = R.drawable.call_out;
break;
case MISSED_TYPE:
str = R.drawable.call_miss;
break;
}
return str;
}

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);
}
}

更多相关文章

  1. Linux下的两种timer方法 (Android 下NDK开发)
  2. Android中VideoView播放当前工程中视频文件的方法
  3. Android Studio 中报错 程序包org.apache.http不存在 的解决方法
  4. Android中使用Makefile编译程序和库的方法
  5. 手把手教你:android调用系统相机、相册功能,适配6.0权限获取以及7.
  6. Google地图(Map)API在J2ME中使用方法
  7. 美团,大众点评,悬浮窗功能代码
  8. Android中FTP上传、下载的功能实现(含进度)
  9. 快速掌握Android 虚拟机(AVD)方法

随机推荐

  1. Android(安卓)Studio:AndroidX的迁移
  2. android app模拟 persistent 属性可以保
  3. 关于安装Android(安卓)Studio的一些问题
  4. 【译】Android开发指南(1)--什么是Androi
  5. 怎样做出一个dialog样式的activity
  6. android控件的对齐方式(转)
  7. Android(安卓)Design 4.4中文版发布
  8. Android界面布局——视图/容器易混淆点总
  9. Android(安卓)SQLite
  10. Android(安卓)TextView属性详解