Java 映像未持久化DataViewAdapter
我有一个正在显示的图像列表。我正在成功获取图像并将图像设置为ImageView 但问题是每当我滚动图像列表时,图像视图往往会加载其他图像视图的图像。i、 e随机更改imageview的图像Java 映像未持久化DataViewAdapter,java,android,image,bitmap,adapter,Java,Android,Image,Bitmap,Adapter,我有一个正在显示的图像列表。我正在成功获取图像并将图像设置为ImageView 但问题是每当我滚动图像列表时,图像视图往往会加载其他图像视图的图像。i、 e随机更改imageview的图像 public View getView(int position, View convertView, ViewGroup parent) { dish = dishes.get(position); LayoutInflater inflater = (LayoutInflater) c
public View getView(int position, View convertView, ViewGroup parent) {
dish = dishes.get(position);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.dish_summary, parent,
false);
image = (ImageView) rowView.findViewById(R.id.dish_image);
Log.i("image",String.valueOf(image.getTag()));
if (image.getTag() == null){
new LoadImage().execute(dish.getImage());
}
notifyDataSetChanged();
return rowView;
}
private class LoadImage extends AsyncTask<String, String, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading Menu ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap img) {
if(img != null)
{
image.setImageBitmap(img);
image.setTag(img);
pDialog.dismiss();
}else
{
pDialog.dismiss();
Toast.makeText(context, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
我想在图像加载后,将其持久化到各自的imageview
public View getView(int position, View convertView, ViewGroup parent) {
dish = dishes.get(position);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.dish_summary, parent,
false);
image = (ImageView) rowView.findViewById(R.id.dish_image);
Log.i("image",String.valueOf(image.getTag()));
if (image.getTag() == null){
new LoadImage().execute(dish.getImage());
}
notifyDataSetChanged();
return rowView;
}
private class LoadImage extends AsyncTask<String, String, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading Menu ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap img) {
if(img != null)
{
image.setImageBitmap(img);
image.setTag(img);
pDialog.dismiss();
}else
{
pDialog.dismiss();
Toast.makeText(context, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
public View getView(int位置、视图转换视图、视图组父视图){
碟=碟。获取(位置);
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
最终视图行视图=充气机。充气(R.layout.dish_摘要,父级,
假);
image=(ImageView)rowView.findviewbyd(R.id.dish\u image);
Log.i(“image”,String.valueOf(image.getTag());
if(image.getTag()==null){
新建LoadImage().execute(dish.getImage());
}
notifyDataSetChanged();
返回行视图;
}
私有类LoadImage扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(上下文);
pDialog.setMessage(“加载菜单…”);
pDialog.show();
}
受保护位图doInBackground(字符串…参数){
试一试{
bitmap=BitmapFactory.decodeStream((InputStream)新URL(args[0]).getContent());
}捕获(例外e){
e、 printStackTrace();
}
返回位图;
}
受保护的void onPostExecute(位图img){
如果(img!=null)
{
设置图像位图(img);
image.setTag(img);
pDialog.disclose();
}否则
{
pDialog.disclose();
Toast.makeText(上下文,“图像不存在或网络错误”,Toast.LENGTH_SHORT.show();
}
}
}
因为每当列表看不见时,会调用GetView()方法来回收/重新绘制列表。图像试图重新绘制自身
为了解决我的问题,我使用了一个外部库,它允许从URL加载图像,并将图像缓存到内存和磁盘以实现超快速加载
希望这有帮助