【问题标题】:None of the methods in RemoteViewsFactory is getting called at all根本没有调用 RemoteViewsFactory 中的任何方法
【发布时间】:2016-06-11 04:37:06
【问题描述】:

我已尽力按照developer.android.com 上的指示进行操作,但似乎我在某个地方出现了一个我无法弄清楚的愚蠢错误。

在连续记录之后,我意识到没有任何方法在我的 RemoteViewsFactory 实现中运行,它(尝试)使用来自内容提供程序光标的数据在小部件中设置 ListView。

RemoteViewsFactory

private Context context;
private int appWidgetId;
private Cursor cursor;

public FootballScoreFactory(Context context, Intent intent){
    this.context = context;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

}

@Override
public void onDataSetChanged() {

    if(cursor != null){
        cursor.close();
    }

    String[] date = new String[1];
    Date filterDate = new Date(System.currentTimeMillis());
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    date[0] = format.format(filterDate);
    try {
        cursor = context.getContentResolver().query(
                Uri.parse([content uri here, tested, returns the data]),
                null,
                null,
                date,
                null
        );
    } catch (Exception e){
        e.printStackTrace();
    }

}
@Override
public RemoteViews getViewAt(int position) {

    String leagueName, home, away, homeGoal, awayGoal;
    leagueName = home = away = homeGoal = awayGoal = "";

    if(cursor.moveToPosition(position)){
        leagueName = cursor.getString(cursor.getColumnIndex(DatabaseContract.scores_table.LEAGUE_COL));
        home = cursor.getString(cursor.getColumnIndex(DatabaseContract.scores_table.HOME_COL));
        away = cursor.getString(cursor.getColumnIndex(DatabaseContract.scores_table.AWAY_COL));
        homeGoal = String.valueOf(cursor.getInt(cursor.getColumnIndex(DatabaseContract.scores_table.HOME_GOALS_COL)));
        awayGoal = String.valueOf(cursor.getInt(cursor.getColumnIndex(DatabaseContract.scores_table.AWAY_GOALS_COL)));
    }

    RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_list_item);
    rv.setTextViewText(R.id.league_name, leagueName);
    rv.setTextViewText(R.id.home, home);
    rv.setTextViewText(R.id.away, away);
    rv.setTextViewText(R.id.home_score, homeGoal.contentEquals("-1")? "-" : homeGoal);
    rv.setTextViewText(R.id.away_score, awayGoal.contentEquals("-1")? "-" : awayGoal);
    return rv;
}

RemoteViewsService

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new FootballScoreFactory(getApplicationContext(), intent);
}

AppWidgetProvider:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds) {
        Intent intent = new Intent(context, FootballScoreService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.football_score);
        rv.setRemoteAdapter(appWidgetId, R.id.widget_list_view, intent);
        rv.setEmptyView(R.id.widget_list_view, R.id.empty_view);

        appWidgetManager.updateAppWidget(appWidgetId, rv);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);

}

任何关于我需要做什么以确保更新视图的帮助都会很棒。如果有帮助,rv.setEmptyView(...); 似乎一直在工作。

我最心碎的是当我认为我没有在应用程序清单文件中绑定视图但看起来我做了。 :(

<service android:name=".FootballScoreService"
        android:permission="android.permission.BIND_REMOTEVIEWS" />

【问题讨论】:

    标签: android android-widget remoteview android-remoteview


    【解决方案1】:

    所以,事实证明我发现了自己的问题。这就像我的 RemoteViewsFactory 没有为 getViewTypeCount() 返回正确的值一样简单。我一开始把它放在0。当我将其更改为 1 时,它起作用了。

    继续,笑我。我做到了。希望这能在某些时候帮助像我这样的可怜的 sole 灵魂。

    如果我再次偶然发现这一点,我不会感到惊讶,哈哈。

    【讨论】:

    • 实际上我不得不说这在 API 中相当混乱,因为没有 getItemViewType 方法。我只是偶然发现了一个案例,我只有 4 个视图类型,但不断收到一个日志条目,我会返回 getViewTypeCount 建议的更多视图类型。所以我随机添加了+1,现在它可以工作了。所以在我看来,实际的 viewType 是由 Launcher/Android 猜到的,如果您对 RemoteViews 实例进行大量修改,它会将其检测为新的 viewType...
    猜你喜欢
    • 1970-01-01
    • 2011-11-18
    • 2013-09-15
    • 2011-03-11
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    相关资源
    最近更新 更多