Android SimpleCursorAdapter.ViewBinder no actualiza TextView enlazado

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;

    }
}

Inconveniente

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 método en 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 llamadas setNotificationUri after executing a query, and the REST server call returns and the thread updates the row the cursor is pointing at.

Gracias de antemano.

preguntado el 01 de julio de 12 a las 01:07

1 Respuestas

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 the ViewBinder como el SimpleCursorAdapter, by default, will set the text for you.

Anyway, I don't understand why you used the columnIndex parameter to identify the views and not the id of the view that is passed in. See if this helps:

public boolean setViewValue(View view, Cursor cursor, int columnIndex) {        
        if (view.getId() == R.id.txtvwProfileUserFullName) {
            String userFullName = cursor.getString(columnIndex);
            ((TextView) view).setText(userFullName);
            return true;
        } else if (view.getId() == R.id.txtvwProfileOtherUserInfo) {
            String userEmail = cursor.getString(columnIndex);
            ((TextView) view).setText(userEmail);
            return true;
        } else {
            return false;
        }
}

Respondido 02 Jul 12, 15:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.