我的应用程序是关于解析xml文件,然后加载来自url和文本的图像并将其显示在列表视图中,但每当从URL加载图像并且滚动列表视图时,它就不能正确加载图像,它强制关闭应用程序。现在如何克服这种情况。请帮助这里是我从网址和文本下载图像的代码。
public class NewsRowAdapter extends ArrayAdapter<Item>
{
LoadingImage loadingImage;
Bitmap bitmap = null;
private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
/*private DisplayImageOptions options;
ImageLoader imageLoader;*/
public NewsRowAdapter(Activity act, int resource, List<Item> arrayList)
{
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.items = arrayList;
/*options = new DisplayImageOptions.Builder().showStubImage(R.drawable.blank).showImageForEmptyUrl(R.drawable.blank).cacheInMemory().cacheOnDisc().build();
imageLoader = ImageLoader.getInstance();*/
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = convertView;
ViewHolder holder;
if (view == null)
{
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
view.setTag(holder);
} else
{
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
holder.tvTitle = (TextView) view.findViewById(R.id.tvtitle);
holder.tvDesc = (TextView) view.findViewById(R.id.tvdesc);
holder.tvDate = (TextView) view.findViewById(R.id.tvdate);
holder.imgView = (ImageView) view.findViewById(R.id.image);
holder.pbar = (ProgressBar) view.findViewById(R.id.pbar);
if (holder.tvTitle != null && null != objBean.getTitle() && objBean.getTitle().trim().length() > 0)
{
holder.tvTitle.setText(Html.fromHtml(objBean.getTitle()));
}
if (holder.tvDesc != null && null != objBean.getDesc()
&& objBean.getDesc().trim().length() > 0) {
holder.tvDesc.setText(Html.fromHtml(objBean.getDesc()));
}
if (holder.tvDate != null && null != objBean.getPubdate() && objBean.getPubdate().trim().length() > 0)
{
holder.tvDate.setText(Html.fromHtml(objBean.getPubdate()));
}
if (holder.imgView != null)
{
if (null != objBean.getLink() && objBean.getLink().trim().length() > 0)
{
final ProgressBar pbar = holder.pbar;
pbar.setVisibility(View.INVISIBLE);
//---------CHANGES MADE FOR LOADING IMAGE----------//
Log.d("IMAGE NULL----------", objBean.getLink());
try
{
URL linkurl = new URL(objBean.getLink());
bitmap = BitmapFactory.decodeStream(linkurl.openConnection().getInputStream());
holder.imgView.setImageBitmap(bitmap);
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
/*try
{
loadBitmap(objBean.getLink());
} catch (IOException e)
{
e.printStackTrace();
}
*/
/*imageLoader.init(ImageLoaderConfiguration.createDefault(activity));
imageLoader.displayImage(objBean.getLink(), holder.imgView, options, new ImageLoadingListener()
{
@Override
public void onLoadingComplete()
{
pbar.setVisibility(View.INVISIBLE);
}
@Override
public void onLoadingFailed()
{
pbar.setVisibility(View.INVISIBLE);
}
@Override
public void onLoadingStarted()
{
pbar.setVisibility(View.VISIBLE);
}
});*/
} else
{
holder.imgView.setImageResource(R.drawable.ic_launcher);
}
}
return view;
}
public class ViewHolder
{
public TextView tvTitle, tvDesc, tvDate;
private ImageView imgView;
private ProgressBar pbar;
}
}
答案 0 :(得分:1)
使用AsyncTask下载图像。 检查一下:http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html
答案 1 :(得分:0)
您正在UI线程上运行网络操作,尝试创建一个AsyncTask do to the job,od a Thread。这很简单,你所要做的就是移动代码。
由于您在适配器上使用此功能,这意味着您将填充项目列表,因此您可能需要查看this。因为你想避免使用listview getView问题的常见问题。
还有一些开源库可以帮助将URL加载到图像中。例如UrlImageViewHelper from Koush可以帮助您做得很好。从您的适配器只需调用:
if (holder.imgView != null) {
if (null != objBean.getLink() && objBean.getLink().trim().length() > 0) {
final ProgressBar pbar = holder.pbar;
pbar.setVisibility(View.INVISIBLE);
//---------CHANGES MADE FOR LOADING IMAGE----------//
Log.d("IMAGE NULL----------", objBean.getLink());
String linkurl = new URL(objBean.getLink().toString());
UrlImageViewHelper.setUrlDrawable(holder.imgView, linkurl, R.id.your_default_drawable);
}