我正在尝试使用 AsyncTask 调整来自Google I/O的模式,以从网络下载图像并将它们放入 AppWidget 中的 ListView 项目中。除了这篇文章之外,我还没有找到很多关于做这种事情的资源。
问题是,该海报没有收藏视图,只有一个项目。当我尝试遵循该模式时,每次 onPostExecute() 运行时,整个 appwidget 都会更新为单个列表项,而不是我需要的。如果您在 onPostExecute() 中删除对 updateAppWidget() 的调用,则视图不会更新,我猜是因为必须发生进程间通信。我不确定从这里去哪里,有人可以帮忙吗?
这是我对 AsyncTask 的定义:
public ThumbnailAsyncTask(RemoteViews rv, int appWidgetID, AppWidgetManager appWidgetManager)
{
mTarget = rv;
this.widgetID = appWidgetID;
this.widgetManager = appWidgetManager;
}
@Override
protected Bitmap doInBackground(String... params)
{
String url = params[0];
return getBitmapFromURL(url);
}
@Override
protected void onPostExecute(Bitmap result)
{
mTarget.setImageViewBitmap(R.id.thumbnail, result);
widgetManager.updateAppWidget(widgetID, mTarget);
}
private Bitmap getBitmapFromURL(String str)
{
Bitmap bmp = null;
HttpURLConnection connection = null;
try
{
URL url = new URL(str);
connection = openConnectionWithTimeout(url);
bmp = BitmapFactory.decodeStream(connection.getInputStream());
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
connection.disconnect();
}
return bmp;
}
}
和来自 RemoteViewsFactory 的 getViewAt():
public RemoteViews getViewAt(int position)
{
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.list_item);
ArrayList<ResultDetails> mResults = mJSONParser.getResults();
if (mResults.get(position).hasHeading())
rv.setTextViewText(R.id.title, mResults.get(position).getHeading());
if (mResults.get(position).hasBody())
rv.setTextViewText(R.id.description, mResults.get(position).getBody(true));
if (mResults.get(position).hasTimestamp())
rv.setTextViewText(R.id.date, mResults.get(position).getTimestamp(true));
Bundle extras = new Bundle();
extras.putString(LINK_ITEM, mResults.get(position).getExternalURL());
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.list_item, fillInIntent);
if (mResults.get(position).hasThumbnailURL())
{
AppWidgetManager mgr = AppWidgetManager.getInstance(mContext);
new ThumbnailAsyncTask(rv, mAppWidgetId, mgr).execute(mResults.get(position).getThumbnailURL());
}
return rv;
}