gpt4 book ai didi

android - 使用 AsyncTask 将图像下载到 AppWidget 中的 ListView

转载 作者:行者123 更新时间:2023-11-30 03:26:22 24 4
gpt4 key购买 nike

我正在尝试调整来自 Google I/O 的模式使用 AsyncTask 从网络下载图像并将它们放入 AppWidget 中的 ListView 项中。除了this post,我还没有找到很多资源来做这样的事情。 .

问题是,那个海报没有 Collection View ,只有一个项目。当我尝试遵循该模式时,每次运行 onPostExecute() 时,整个 appwidget 只更新一个列表项,而不是我需要的。如果您在 onPostExecute() 中删除对 updateAppWidget() 的调用,则 View 不会更新,我猜是因为必须发生的进程间通信。我不太确定从这里到哪里去,有人可以帮忙吗?

这是我对 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;
}

最佳答案

我对加载时间长不满意,所以我想出了一种方法来快速加载占位符图像,然后在 AsyncTask 中加载缩略图,然后调用一次 notifyAppWidgetViewDataChanged() 来刷新 appwidget全部加载。这会导致在没有图像的情况下立即显示数据,这对我的应用程序来说是一个非常好的解决方案。我在我的 RemoteViewsFactory 中使用了一个 bool 值来指示我是否已经下载了没有图像的数据,这样它就不会陷入无休止的刷新周期。

class ThumbnailAsyncTask extends AsyncTask<Void, Void, Void>
{
private AppWidgetManager widgetManager;
private List<ResultDetails> details;
private Context context;

public ThumbnailAsyncTask(AppWidgetManager
appWidgetManager, List<ResultDetails> details, Context context)
{
this.details = details;
this.context = context;
this.widgetManager = appWidgetManager;
}

@Override
protected Void doInBackground(Void... params)
{
for (ResultDetails item : details)
{
if (item.hasThumbnailURL())
{
item.setThumbnail(getBitmapFromURL(item.getThumbnailURL()));
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
widgetManager.notifyAppWidgetViewDataChanged(widgetManager.getAppWidgetIds(
new ComponentName(context, WidgetProvider.class)), R.id.list_view);

}

public RemoteViews getViewAt(int position)
{
RemoteViews rv;
rv = new RemoteViews(mContext.getPackageName(), R.layout.list_item);
ArrayList<ResultDetails> mResults = mSearchResultsGenerator.getResults();
ResultDetails result = mResults.get(position);

if (result.hasHeading())
rv.setTextViewText(R.id.title, result.getHeading());
if (result.hasBody())
rv.setTextViewText(R.id.description, result.getBody(true));
if (result.hasTimestamp())
rv.setTextViewText(R.id.date, result.getTimestamp(true));
if (result.hasPrice())
rv.setTextViewText(R.id.price, result.getPrice());

Bundle extras = new Bundle();
extras.putString(LINK_ITEM, result.getExternalURL());
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.list_item, fillInIntent);

if (result.hasThumbnail())
{
rv.setImageViewBitmap(R.id.thumbnail, result.getThumbnail());
}
else
{
rv.setImageViewBitmap(R.id.thumbnail, noImageBitmap);
}

return rv;
}
public void onDataSetChanged()
{
if (isOnline(mContext))
{
if (!hasData)
{
mSearchResultsGenerator.generate();
if (mSearchResultsGenerator.isUpdateSucceeded())
{
AppWidgetManager mgr = AppWidgetManager.getInstance(mContext);
hasData = true;
new ThumbnailAsyncTask(mgr, mSearchResultsGenerator.getResults(), mContext).execute();
}
else
makeNoConnectionToast();
}
else
{

}
}
else
makeNoConnectionToast();
}

关于android - 使用 AsyncTask 将图像下载到 AppWidget 中的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167111/

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