CursorLoader和CursorAdapter的配合使用
来源:互联网 发布:linux 移动整个文件夹 编辑:程序博客网 时间:2024/05/17 22:19
Android 3.0引入了CursorLoader目的在于能够更好的管理cursor的打开使用和关闭以及数据变化时的自动更新ListView的填充item。
简单的使用过程例子:
1.在activity中:
public class ContactActivity extents Activity {
private ListView mListView;
private ContactAdapter mContactAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mediaphone_contact_details);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mediaphone_contact_details);
this.mListView = (ListView)findViewById(R.id.lv_call_details);
mCallLogAdapter = new CallLogAdapter(this, null);
//getLoaderManager()是loader的管理方法用于初始化loader
getLoaderManager().initLoader(0, null, new CallLogLoaderCallback());
mListView.setAdapter(mCallLogAdapter);
}
private calss CallLogLoaderCallback implements LoaderCallbacks<Cursor> {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
//返回一个cursor的对象 此时是初始化cursor
return new CursorLoader(
ContactDetailsActivity.this,
SipCallLogs.CALLLOG_CONTENT_URI,//uri
SipCallLogs.CALLLOGS_FULL_PROJECTION,//查询的字段
SipCallLogs.COLUMN_NAME_NUMBER + "=?", //查询的田间
new String[]{contact.getExtNumber()}, //查询条件的参数
SipCallLogs.DEFAULT_SORT_ORDER);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
return new CursorLoader(
ContactDetailsActivity.this,
SipCallLogs.CALLLOG_CONTENT_URI,//uri
SipCallLogs.CALLLOGS_FULL_PROJECTION,//查询的字段
SipCallLogs.COLUMN_NAME_NUMBER + "=?", //查询的田间
new String[]{contact.getExtNumber()}, //查询条件的参数
SipCallLogs.DEFAULT_SORT_ORDER);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mContactAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mContactAdapter.swapCursor(null);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mContactAdapter.swapCursor(null);
}
private class ContactAdapter extends CursorAdapter {
private ViewItemHolder mHolder;
private Context mContext;
public ContactAdapter(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_contact_details_callog_list_item, parent, false);
int pos = cursor.getPosition();
cursor.moveToFirst();
while(!cursor.isAfterLast()){
int cal = cursor.getInt(SipCallLogs.FULL_COLUMN_INDEX_TYPE);
String str = cursor.getString(cursor.getColumnIndex(SipCallLogs.COLUMN_NAME_TYPE));
Log.i("tag", str + " *********************************"+cal);
cursor.moveToNext();
}
cursor.moveToPosition(pos);
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);
int callType = cursor.getInt(cursor.getColumnIndex(SipCallLogs.COLUMN_NAME_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);//通过此方法的好处在于,当listview的点击事件时,可以直接取到tag里面的数字如:在OnItemClick中可以这样写:ViewItemHolder holderView = (ViewItemHolder) view.getTag(); String data = holderView.mDataContent.getTag().toString();等等
}
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);
}
}
}
private ViewItemHolder mHolder;
private Context mContext;
public ContactAdapter(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_contact_details_callog_list_item, parent, false);
int pos = cursor.getPosition();
cursor.moveToFirst();
while(!cursor.isAfterLast()){
int cal = cursor.getInt(SipCallLogs.FULL_COLUMN_INDEX_TYPE);
String str = cursor.getString(cursor.getColumnIndex(SipCallLogs.COLUMN_NAME_TYPE));
Log.i("tag", str + " *********************************"+cal);
cursor.moveToNext();
}
cursor.moveToPosition(pos);
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);
int callType = cursor.getInt(cursor.getColumnIndex(SipCallLogs.COLUMN_NAME_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);//通过此方法的好处在于,当listview的点击事件时,可以直接取到tag里面的数字如:在OnItemClick中可以这样写:ViewItemHolder holderView = (ViewItemHolder) view.getTag(); String data = holderView.mDataContent.getTag().toString();等等
}
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);
}
}
}
- CursorLoader和CursorAdapter的配合使用
- ListView+CursorAdapter+LoaderManger的配合使用
- CursorLoader的使用
- CursorLoader的使用
- CursorAdapter和AsyncQueryHandler的简易使用
- CursorAdapter类的使用
- Android CursorAdapter的使用
- CursorAdapter的使用
- Android中LoaderManager:AsyncTaskLoader和CursorLoader的使用
- 自动读取短信验证码LoaderManager和CursorLoader的使用
- 使用CursorAdapter的注意啦
- listview上的CursorAdapter使用
- CursorAdapter使用
- BaseAdapter与CursorAdapter的结合使用举例
- Android中CursorAdapter的使用详解
- CursorAdapter的使用:获取手机联系人
- Android中CursorAdapter的使用详解
- Android cursor和cursorAdapter 的监听机制
- MyEclipse5.0M1注册码(永久生效)
- 一种实用的笑脸检测方法
- 基于Linux开源VOIP系统LinPhone[二]
- TinyOS学习笔记13-节点与计算机利用串口通信3-MIG分析
- 前端JS模板
- CursorLoader和CursorAdapter的配合使用
- 高性能JavaScript模板引擎原理解析
- mysql中主键自动增长时的几种情况
- 前段/oa开发必备jquery ui
- 用拦截器解决页面JSP缓存问题
- Oracle建立DBLINK的详细步骤记录
- 把文件用base64进行转码
- c语言调用库函数qsort()进行快速排序
- Android ListView的cell的分割线