我正在学习使用带有集合的小部件。我正在异步加载来自互联网的分数,在加载数据后我想更新视图但是,我的代码跳过更新视图部分。我已经尝试了几件事情,但它并没有等待异步完成它会在列表填充之前调用其他回调方法。
WidgetDataProvider.class
public class WidgetDataProvider implements RemoteViewsService.RemoteViewsFactory {
List<SportScores> mSportScores;
Context mContext = null;
public WidgetDataProvider(Context context, Intent intent) {
mContext = context;
mSportScores = new ArrayList<>();
}
@Override
public void onCreate() {
initData();
Log.i(FetchServiceConstants.WIDGET, "SPORT SCORES SIZE = " + mSportScores.size());
}
@Override
public void onDataSetChanged() {
initData();
}
@Override
public void onDestroy() {
}
@Override
public int getCount() {
return mSportScores.size();
}
@Override
public RemoteViews getViewAt(int position) {
Log.i(FetchServiceConstants.WIDGET, "GET VIEW AT");
RemoteViews mRemoteViews = new RemoteViews(mContext.getPackageName(),
R.layout.football_scores_widget);
mRemoteViews.setTextViewText(R.id.home_name_widget,
mSportScores.get(position).getHomeName());
mRemoteViews.setTextViewText(R.id.away_name_widget, mSportScores.get(position).getAwayName());
mRemoteViews.setTextViewText(R.id.score_textview_widget, mSportScores.get(position).getScore());
mRemoteViews.setImageViewBitmap(R.id.away_crest_widget, BitmapFactory.decodeResource(
mContext.getResources(), Utilies.getTeamCrestByTeamName(mSportScores.get(position).getAwayName())
));
mRemoteViews.setImageViewBitmap(R.id.home_crest_widget, BitmapFactory.decodeResource(
mContext.getResources(), Utilies.getTeamCrestByTeamName(mSportScores.get(position).getHomeName())
));
mRemoteViews.setTextColor(R.id.home_name_widget, Color.BLACK);
mRemoteViews.setTextColor(R.id.score_textview_widget, Color.BLACK);
mRemoteViews.setTextColor(R.id.data_textview_widget, Color.BLACK);
mRemoteViews.setTextColor(R.id.away_name_widget, Color.BLACK);
// mRemoteViews.setOnClickPendingIntent(R.id.list_item_layout_widget, pendingIntent);
return mRemoteViews;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return false;
}
private void initData() {
mSportScores.clear();
new async().execute();
Log.i(FetchServiceConstants.WIDGET, "intit DATA ");
}
private Cursor getTodaysScores() {
Log.i(FetchServiceConstants.WIDGET, "todays scores");
return mContext.getContentResolver().query(
DatabaseContract.scores_table.buildScoreWithDate(),
null,
null,
new String[]{PagerFragment.getDate(4)},
null);
}
private void addScoresToList(Cursor scoreCursor) {
Log.i(FetchServiceConstants.WIDGET, "Loggint info");
if (scoreCursor == null) {
Log.i(FetchServiceConstants.WIDGET, "cursor = null");
} else {
Log.i(FetchServiceConstants.WIDGET, scoreCursor.getCount() + "");
}
scoreCursor.moveToFirst();
while (!scoreCursor.isAfterLast()) {
SportScores newSportScore = new SportScores(
scoreCursor.getString(scoresAdapter.COL_HOME),
scoreCursor.getString(scoresAdapter.COL_AWAY),
Utilies.getScores(
scoreCursor.getInt(scoresAdapter.COL_HOME_GOALS),
scoreCursor.getInt(scoresAdapter.COL_AWAY_GOALS))
);
mSportScores.add(newSportScore);
}
}
private void update_scores() {
Intent service_start = new Intent(mContext, myFetchService.class);
mContext.startService(service_start);
}
private class async extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
update_scores();
addScoresToList(getTodaysScores());
Log.i(FetchServiceConstants.WIDGET, "in back");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
//update remote views after scores have been updated
//NOT SURE WHAT TODO HERE
}
}
}
WidgetService.class
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
WidgetDataProvider dataProvider = new WidgetDataProvider(
getApplicationContext(), intent);
return dataProvider;
}
}
FootballScoresWidget.class
public class FootballScoresWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int widgetId : appWidgetIds) {
RemoteViews mView = initViews(context, appWidgetManager, widgetId);
appWidgetManager.updateAppWidget(widgetId, mView);
Log.i(FetchServiceConstants.WIDGET, "ON UPDATE ");
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@SuppressWarnings("deprecation")
private RemoteViews initViews(Context context,
AppWidgetManager widgetManager, int widgetId) {
RemoteViews mView = new RemoteViews(context.getPackageName(),
R.layout.football_widget_list);
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
Log.i(FetchServiceConstants.WIDGET, "intit views ");
mView.setRemoteAdapter(widgetId, R.id.widgetCollectionList, intent);
return mView;
}
}
如何在加载数据后更新小部件列表视图,或者有更好的方法来完成我想要做的事情?