我尽力遵循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" />
所以,结果我发现了自己的问题。就像我的RemoteViewsFactory没有为getViewTypeCount()返回正确的值一样简单。我一开始就把它留在了0。当我把它改为1时,它起作用了。
继续,嘲笑我。我做到了。希望这有助于一些穷人 唯一 灵魂在某种程度上像我一样。
如果我偶然发现这一点,我不会感到惊讶大声笑。