我已尽力按照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时,它起作用了。
继续,嘲笑我。是的希望这可以在某些时候帮助像我这样的可怜的唯一灵魂。
如果我再次偶然发现这一点,我不会感到惊讶。