我的CursorAdapter在我使用managedQuery
时加载,但在我使用LoaderFramework
时则不加载。它会加载一个空白屏幕。
我的ListFragment的代码:
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
adapter=new ContactsAdapter(getActivity(),null);
setEmptyText("No Contacts found");
setListShown(false);
setListAdapter(adapter);
getLoaderManager().initLoader(LOADER_CODE, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// TODO Auto-generated method stub
String[] projections=new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.LOOKUP_KEY};
String orderBy=ContactsContract.Contacts.TIMES_CONTACTED+" DESC LIMIT 5";
CursorLoader loader=new CursorLoader(getActivity(),ContactsContract.Contacts.CONTENT_URI,projections,null,null,orderBy);
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
// TODO Auto-generated method stub
adapter.swapCursor(c);
if(isResumed())
setListShown(true);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// TODO Auto-generated method stub
adapter.swapCursor(null);
}
我的适配器的newView
和bindView
方法如下所示:
@Override
public void bindView(View view, Context context, Cursor c) {
// TODO Auto-generated method stub
String nameColumn;
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.HONEYCOMB)
{
nameColumn=ContactsContract.Contacts.DISPLAY_NAME_PRIMARY;
}
else
{
nameColumn=ContactsContract.Contacts.DISPLAY_NAME;
}
QuickContactBadge badge=(QuickContactBadge)view.getTag(R.id.rowbadger);
TextView text_name=(TextView)view.getTag(R.id.text_name);
TextView text_phone=(TextView)view.getTag(R.id.text_phone);
long id=c.getLong(c.getColumnIndex(ContactsContract.Contacts._ID));
Uri lookupUri=Contacts.getLookupUri(id,c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)));
badge.assignContactUri(lookupUri);
Bitmap bitmap=loadContactPhoto(context.getContentResolver(), id);
badge.setImageBitmap(bitmap);
text_name.setText(c.getString(c.getColumnIndex(nameColumn)));
text_phone.setText(getContactPhoneNumber(id));
}
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
// TODO Auto-generated method stub
View v= inflater.inflate(R.layout.layout_row, parent,false);
v.setTag(R.id.rowbadger,v.findViewById(R.id.rowbadger));
v.setTag(R.id.text_name,v.findViewById(R.id.text_name));
v.setTag(R.id.text_phone,v.findViewById(R.id.text_phone));
return v;
}