使用cursorLoader的示例应用,我尝试更新我的代码以获取由电话的已加星标的联系人姓名和照片填充的列表视图。问题是列表视图没有出现,我没有收到任何错误。这是我的相关代码:
ContactInfoListAdapter:
public class ContactInfoListAdapter extends ResourceCursorAdapter {
private final ContentResolver mContentResolver;
private Bitmap mNoPictureBitmap;
private String TAG = "ContactInfoListAdapter";
public ContactInfoListAdapter(Context context, int layout, Cursor c,
int flags) {
super(context, layout, c, flags);
mContentResolver = context.getContentResolver();
// Default thumbnail bitmap for when contact has no thubnail
mNoPictureBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.defcontpic);
}
// Called when a new view is needed
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.favs_list_item, parent, false);
}
// Called when a new data view is needed, but an old view is
// available for reuse
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Set display name
TextView textView = (TextView) view.findViewById(R.id.tvContactName);
textView.setText(cursor.getString(cursor
.getColumnIndex(Contacts.DISPLAY_NAME)));
// Set default thumbnail
ImageView imageView = (ImageView) view.findViewById(R.id.ivDefContact);
Bitmap photoBitmap = mNoPictureBitmap;
// Try to set actual thumbnail, if it's available
String photoContentUri = cursor.getString(cursor
.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
if (null != photoContentUri) {
InputStream input = null;
try {
// read thumbail data from memory
input = mContentResolver.openInputStream(Uri
.parse(photoContentUri));
if (input != null) {
photoBitmap = BitmapFactory.decodeStream(input);
}
} catch (FileNotFoundException e) {
Log.i(TAG, "FileNotFoundException");
}
}
imageView.setImageBitmap(photoBitmap);
}
}
的活动:
public class DialerActivity extends Activity implements View.OnClickListener View.OnLongClickListener,
AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>{
private ContactInfoListAdapter starAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialer_layout);
WindowManager.LayoutParams wmlp = getWindow().getAttributes();
wmlp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
overridePendingTransition(R.animator.push_right_in,0);
Initialise();
starAdapter = new ContactInfoListAdapter(this, R.layout.favs_list_item, null, 0);
listStarred = (ListView) findViewById(R.id.lvFavs);
listStarred.setAdapter(starAdapter);
listStarred.setOnItemClickListener(this);
// Initialize the loader
getLoaderManager().initLoader(0, null, this);
}
static final String[] CONTACTS_ROWS = new String[] { Contacts._ID,
Contacts.DISPLAY_NAME, Contacts.PHOTO_THUMBNAIL_URI };
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.DISPLAY_NAME + " != '' ) AND (" + Contacts.STARRED
+ "== 1))";
// String used for defining the sort order
String sortOrder = Contacts._ID + " ASC";
return new CursorLoader(this, Contacts.CONTENT_URI, CONTACTS_ROWS,
select, null, sortOrder);
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
starAdapter.swapCursor(cursor);
starAdapter.notifyDataSetChanged();
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
starAdapter.swapCursor(null);
}
dialer_layout(是一个relativeLayout,其中包含listview所在的LinearLayout):
<LinearLayout
android:id="@+id/llFavs"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/b3"
android:layout_alignParentEnd="false"
android:layout_marginBottom="8dp"
android:layout_marginTop="5dp"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lvFavs"/>
</LinearLayout>
favs_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="114dp">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:id="@+id/ivDefContact"
android:src="@drawable/defcontpic"
android:layout_marginLeft="5dp"/>
<TextView
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="..........."
android:id="@+id/tvContactName"
android:gravity="center"
android:layout_marginLeft="5dp"
android:textSize="12sp"/>
</LinearLayout>
答案 0 :(得分:0)
没关系我解决了,这是一个愚蠢的排版错误
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
starAdapter.swapCursor(cursor);
starAdapter.notifyDataSetChanged();
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
starAdapter.swapCursor(null);
onLoadFinished方法中的swapCursor应该是&#34; arg1&#34;而不是&#34;光标&#34;。不幸的是,我已经有一个变量&#34;光标&#34;并且Eclipse没有弹出任何错误。我失去了2个小时。