android - 根本没有调用 RemoteViewsFactory 中的任何方法

标签 android android-widget remoteview android-remoteview

我已尽力按照 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);

}

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

我最大的心碎是当我认为我没有绑定(bind)应用程序 list 文件中的 View 时,但看起来我绑定(bind)了。 :(

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

最佳答案

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

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

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

关于android - 根本没有调用 RemoteViewsFactory 中的任何方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35677533/

相关文章:

android - 面向 Android 12 及更高版本的应用需要为 'android:exported' 指定显式值

android - OnUpdate() 不调用小部件服务

android - 安卓音乐播放器的通知

android - 在 Activity 中嵌入 Google 日历

android - 如何设置自定义 ListAdapter 以在 appwidget 中列出 View ?

java - 动态添加的 RemoteView 的布局权重

android - 如何在Gradle中声明变量并将其打印在另一个Gradle文件中

android - 在 xml 中将 xml 作为消息发送

Android Canvas.drawPicture 不适用于带有 Ice Cream Sandwich 的设备

android - 处理键盘中的 'Done' 事件不起作用