简体   繁体   English

Android SimpleCursorAdapter.ViewBinder不更新绑定的TextView

[英]Android SimpleCursorAdapter.ViewBinder not updating bound TextView

I have a SimpleCursorAdapter and I'm attempting to bind a SimpleCursorAdapter.ViewBinder to it. 我有一个SimpleCursorAdapter,我正在尝试将SimpleCursorAdapter.ViewBinder绑定到它。 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: 我的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;

    }
}

Issue 问题

When I load the activity, the TextView's do not get updated with the data from the userCursor . 当我加载活动时,TextView不会使用来自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. 我可以确认userCursor实际上指向支持SQLite数据库中的有效行,并且有数据,但UserProfileViewBindersetViewValue方法似乎永远不会执行。 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: 作为替代方案,我正在执行以下操作来设置TextView的文本:

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. 但我不认为这会在光标数据发生变化时自动更新TextView。 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. ContentProvider在执行查询后调用setNotificationUri ,并且REST服务器调用返回并且线程更新光标指向的行。

Thanks in advance. 提前致谢。

Are you planing to do other thing besides setting the text on those two TextViews ? 除了在这两个TextViews上设置文本之外,您是否计划做其他事情? If the answer is no then you have no reason to use the ViewBinder as the SimpleCursorAdapter , by default, will set the text for you. 如果答案为否,那么您没有理由使用ViewBinder作为SimpleCursorAdapter ,默认情况下,将为您设置文本。

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: 无论如何,我不明白为什么你使用columnIndex参数来识别视图而不是传入的视图的id。看看这是否有帮助:

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;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM