I have a SimpleCursorAdapter and I’m attempting to bind a SimpleCursorAdapter.ViewBinder to it. Below is the code that I am using.
// Setup Cursor and SimpleCursorAdapter, called in Activity onResume()
Cursor userCursor = getUserProfile(userEmail);
mAdapter = new SimpleCursorAdapter(this,
R.layout.user_profile,
userCursor,
new String[] {
StackOverFlow.Users.FULL_NAME,
StackOverFlow.Users.EMAIL },
new int[] {
R.id.txtvwProfileUserFullName,
R.id.txtvwProfileOtherUserInfo } );
mAdapter.setViewBinder(new UserProfileViewBinder());
My UserProfileViewBinder class:
private class UserProfileViewBinder implements SimpleCursorAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
TextView tv;
switch (columnIndex) {
case 1: // TODO: Magic Numbers, bad!
tv = (TextView) view.findViewById(R.id.txtvwProfileUserFullName);
String userFullName = cursor.getString(columnIndex);
tv.setText(userFullName);
break;
case 2: // TODO: Magic Numbers, bad!
tv = (TextView) view.findViewById(R.id.txtvwProfileOtherUserInfo);
String userEmail = cursor.getString(columnIndex);
tv.setText(userEmail);
break;
}
return true;
}
}
Issue
When I load the activity, the TextView’s do not get updated with the data from the userCursor. I can confirm that userCursor is in fact pointing to a valid row in the backing SQLite database, and has data, but the setViewValue method in UserProfileViewBinder never appears to be executing. I’m not sure what I’m doing wrong, or neglecting to include.
As an alternative, I’m doing the following to set the TextView’s text:
if (mAdapter.getCursor().moveToFirst()) {
mUserFullName.setText(mAdapter.getCursor().getString(
StackOverFlow.USER_FULL_NAME));
mUserOtherInfo.setText(mAdapter.getCursor().getString(
StackOverFlow.USER_EMAIL));
}
But I don’t think this will automatically update the TextView’s when the cursor data changes. when the ContentProvider calls setNotificationUri after executing a query, and the REST server call returns and the thread updates the row the cursor is pointing at.
Thanks in advance.
Are you planing to do other thing besides setting the text on those two
TextViews? If the answer is no then you have no reason to use theViewBinderas theSimpleCursorAdapter, by default, will set the text for you.Anyway, I don’t understand why you used the
columnIndexparameter to identify the views and not the id of the view that is passed in. See if this helps: