CursorLoader和CursorAdapter的配合使用

       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);
                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) {
//返回一個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);
}


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);
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章