class CallLogAdapter extends CursorAdapter {
private ViewItemHolder mHolder;
private Context mContext;
public CallLogAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
this.mContext = context;
}
final class ViewItemHolder {
TextView callDate;
TextView callTime;
ImageView callType;
TextView duration;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.mediaphone_simple_callog_list_item, parent, false);
createChildViews(view, cursor);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
createChildViews(view, cursor);
}
private void createChildViews(View view, Cursor cursor) {
mHolder = (ViewItemHolder) view.getTag();
if(mHolder == null){
mHolder = new ViewItemHolder();
mHolder.callDate = (TextView) view.findViewById(R.id.tv_call_date);
mHolder.callTime = (TextView) view.findViewById(R.id.tv_call_time);
mHolder.callType = (ImageView) view.findViewById(R.id.iv_call_type);
mHolder.duration = (TextView) view.findViewById(R.id.tv_call_duration);
}
long datetime = cursor.getLong(SipCallLogs.FULL_COLUMN_INDEX_DATE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(datetime);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
String dateStr = dateFormat.format(cal.getTime());
String timeStr = timeFormat.format(cal.getTime());
mHolder.callDate.setText(dateStr);
mHolder.callTime.setText(timeStr);
int callType = cursor.getInt(SipCallLogs.FULL_COLUMN_INDEX_TYPE);
Drawable callTypeImg = null;
if (Calls.INCOMING_TYPE == callType) {
callTypeImg = mContext.getResources().getDrawable(R.drawable.ic_call_type_income);
} else if (Calls.OUTGOING_TYPE == callType) {
callTypeImg = mContext.getResources().getDrawable(R.drawable.ic_call_type_outgo);
} else if (Calls.MISSED_TYPE == callType) {
callTypeImg = mContext.getResources().getDrawable(R.drawable.ic_call_type_income_miss);
}
mHolder.callType.setImageDrawable(callTypeImg);
mHolder.duration.setText(formatDuration(cursor.getLong(SipCallLogs.FULL_COLUMN_INDEX_DURATION)));
view.setTag(mHolder);
}
private String formatDuration(long elapsedSeconds) {
long minutes = 0;
long seconds = 0;
if (elapsedSeconds >= 60) {
minutes = elapsedSeconds / 60;
elapsedSeconds -= minutes * 60;
}
seconds = elapsedSeconds;
return mContext.getString(R.string.callDetailsDurationFormat, minutes, seconds);
}
}
CursorAdapter
最新推荐文章于 2021-05-29 02:57:46 发布