Android SimpleCursorAdapter.ViewBinder no actualiza TextView enlazado
Frecuentes
Visto 1,139 equipos
1
Tengo un SimpleCursorAdapter y estoy intentando enlazarlo con un SimpleCursorAdapter.ViewBinder. A continuación se muestra el código que estoy usando.
// 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());
Mi clase UserProfileViewBinder:
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
Cuando cargo la actividad, los TextView no se actualizan con los datos del userCursor
. puedo confirmar eso userCursor
de hecho, apunta a una fila válida en la base de datos SQLite de respaldo y tiene datos, pero el setViewValue
método en UserProfileViewBinder
nunca parece estar ejecutándose. No estoy seguro de lo que estoy haciendo mal o de lo que estoy dejando de incluir.
Como alternativa, estoy haciendo lo siguiente para configurar el texto de TextView:
if (mAdapter.getCursor().moveToFirst()) {
mUserFullName.setText(mAdapter.getCursor().getString(
StackOverFlow.USER_FULL_NAME));
mUserOtherInfo.setText(mAdapter.getCursor().getString(
StackOverFlow.USER_EMAIL));
}
Pero no creo que esto actualice automáticamente TextView cuando cambien los datos del cursor. cuando el ContentProvider
llamadas setNotificationUri
después de ejecutar una consulta, y la llamada del servidor REST regresa y el subproceso actualiza la fila a la que apunta el cursor.
Gracias de antemano.
1 Respuestas
3
¿Está planeando hacer otra cosa además de configurar el texto en esos dos TextViews
? Si la respuesta es no, entonces no tienes razón para usar el ViewBinder
como el SimpleCursorAdapter
, de forma predeterminada, establecerá el texto por usted.
De todos modos, no entiendo por qué usaste el columnIndex
parámetro para identificar las vistas y no la identificación de la vista que se pasa. Vea si esto ayuda:
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 java android sqlite or haz tu propia pregunta.