gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:23:27 31 4
gpt4 key购买 nike

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

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com