0

I've been trying to update my Images GridView.
The images get updated only if I set the adapter again. Setting the adapter again makes my activity scroll back to top and I try to avoid that.

Here's some code:

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;

    }
}

Now, this code works for updating the views:

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

But this doesnt:

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

What could be the reason for not updating my views? Thank you!

EDIT: The ImageAdapter code:

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;
    }
}
4

1 回答 1

1

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

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

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

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

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

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;
  }
于 2013-07-13T11:11:33.507 回答