图像GridView不会失效

时间:2013-07-13 10:40:01

标签: android image gridview android-asynctask android-arrayadapter

我一直在尝试更新我的图片GridView 仅当我再次设置适配器时,图像才会更新。再次设置适配器使我的活动滚动到顶部,我尽量避免这种情况。

以下是一些代码:

private class LoadImagesTask extends AsyncTask<Object,Void, Object> {
    private ImageAdapter imageAdapter;
    private ArrayList<Integer> idsArray;
    @Override
    protected void onPreExecute() {   
        imageAdapter = new ImageAdapter(getApplicationContext());
        Bitmap bmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.locked_image);
        idsArray = dbHelper.getPicturesIdsByAlbumId(1);
        for(int i = 0;i<idsArray.size();i++){
            imageAdapter.bitmaps.add(bmap);
        }

        gridView.setAdapter(imageAdapter);
    }
    @Override
    protected Void doInBackground(Object... arg) {

        for(int i = 0;i<idsArray.size();i++){
            byte[] data = dbHelper.getPictureById(idsArray.get(i)).littlePicBytes;
            imageAdapter.bitmaps.set(i, BitmapFactory.decodeByteArray(data, 0, data.length));
            publishProgress(); //********** This line calls onProgressUpdate()  *********
        }
        return null;

    }
}

现在,此代码适用于更新视图:

@Override
protected void onProgressUpdate(Void... params) {
    gridView.setAdapter(imageAdapter);
}

但这并不是:

@Override
protected void onProgressUpdate(Void... params) {
    imageAdapter.notifyDataSetChanged();
    gridView.invalidateViews();
}

不更新我的观点可能是什么原因?谢谢!

编辑: ImageAdapter代码:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
    public ImageAdapter(Context c) {
        mContext = c;

    }

    public int getCount() {
        return bitmaps.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View gridView;

            if (convertView == null) {

                gridView = new View(mContext);

                // get layout from mobile.xml
                gridView = inflater.inflate(R.layout.image_adapter, null);

                // set image based on selected text
                ImageView imageView = (ImageView) gridView
                        .findViewById(R.id.imageView);
                imageView.setImageBitmap(bitmaps.get(position));
            }
                else{ 
                    gridView = (View) convertView;
                }
            return gridView;
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码中存在两个主要问题:

    在doInBackground中,
  1. 更改了适配器的数据,这不是线程安全的操作,因为适配器可能会在您更改时从数据中读取。我正在谈论这一行:

    imageAdapter.bitmaps.set(i, BitmapFactory.decodeByteArray(data, 0, data.length));
    

    相反,您应该仅在UI线程上更新其数据,例如在onProgressUpdate内部。

  2. 在getView中
  3. ,您已经错误地实现了它。当convertView不为null时,您永远不会更新imageView的内容。应该是什么样的:

  4. public View getView(int position, View convertView, ViewGroup parent) 
      {
      View rootView=convertView;
      ViewHolder holder;
      if (rootView != null) 
        holder=(ViewHolder) rootView.getTag();
      else
        {
        rootView = inflater.inflate(R.layout.image_adapter, null,false);
        holder = new ViewHolder();
        holder.imageView = (ImageView) rootView.findViewById(R.id.imageView);
        rootView.setTag(holder);
        }
      holder.imageView.setImageBitmap(getItem(position));
      return rootView;
      }
    
    
    private Bitmap getItem(final int position)
      {
      return bitmaps.get(position); 
      }    
    
    private static class ViewHolder
      {
      ImageView imageView;
      }