嗨,我正在开发一个 android 应用程序,我试图在列表视图上显示通话记录。
private void getCallDetails()
{
Cursor managedCursor = getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,null, "date DESC");
int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
int nameid = managedCursor.getColumnIndex( CallLog.Calls.CACHED_NAME);
while (managedCursor.moveToNext())
{
HashMap<String, String> map = new HashMap<String, String>();
phNumber = managedCursor.getString( number );
//type of call
String callType = managedCursor.getString( type );
//call date
String callDate = managedCursor.getString( date );
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM");
String dateString = formatter.format(new Date(Long.valueOf(callDate)));
//call time
SimpleDateFormat time = new SimpleDateFormat("HH:mm");
String timeString = time.format(new Date(Long.valueOf(callDate)));
String calldur = managedCursor.getString(duration);
int sec = Integer.parseInt(calldur);
//converting seconds
String dur = String.format("%02d:%02d:%02d", sec/3600, (sec%3600)/60, (sec%60));
String name = managedCursor.getString(nameid);
if(name == null)
{
map.put(contactName, "Unknown");
}
else
{
map.put(contactName, name);
}
int dir = 0;
int imageId = 0;
int dircode = Integer.parseInt( callType );
switch( dircode )
{
case CallLog.Calls.OUTGOING_TYPE:
dir = R.drawable.outgoing;
imageId = R.drawable.clock;
callDuration = dur;
break;
case CallLog.Calls.INCOMING_TYPE:
dir = R.drawable.incoming;
imageId = R.drawable.clock;
callDuration = dur;
break;
case CallLog.Calls.MISSED_TYPE:
dir = R.drawable.missed;
callDuration = " ";
imageId = R.drawable.empty;
break;
}
map.put(phone, phNumber);
map.put(DateOfCall,dateString );
map.put(TimeOfCall,timeString );
dirType = String.valueOf(dir);
map.put(typeofCall,dirType );
map.put(durationOfCall, callDuration);
clkimgId = String.valueOf(imageId);
map.put(imgId,clkimgId);
map.put(image,contactID);
DetailsList.add(map);
}
managedCursor.close();
}
我在 doInBackground 中调用上述 getCallDetails() 并在 onPostExecute 中加载到适配器
adapter = new Adapter(Cal.this, DetailsList);
callList.setAdapter(adapter);
但是这个加载太慢了。我该如何解决?不知道我哪里出错了。
请帮忙。谢谢!