如何动态更改gridview中imageview的图像位图

时间:2014-02-13 09:23:28

标签: android gridview android-arrayadapter

我已经更改了LoadThumbOperation代码,我正在更改缩略图,但我得到的标记为null, 如果我不将标签更改为view.setTag(null);缩略图没有改变。但我需要后者标签,所以我不能将其更改为null。有没有其他方法可以做到这一点,我会改变拇指,标签不设置为空

LoadThumbOperation.java

public class LoadThumbOperation extends AsyncTask<String, Bitmap, String> {

Activity activity;
GridView myGridView;
View view;
int count, position;
ArrayList<Integer> ids;

public LoadThumbOperation(Activity _activity) {
    super();
    this.activity = _activity;
    try {
        myGridView = (GridView) activity.findViewById(R.id.gridViewMedia);
    } catch (Exception e) {
        LocalLogger.LOGGER.error("Error while finding gridview "
                + e.getMessage());
        e.printStackTrace();
    }
}

@Override
protected void onPreExecute() {
    count = myGridView.getChildCount();
    ids = new ArrayList<Integer>();
    for (int i = 0; i < count; i++) {
        view = myGridView.getChildAt(i);
        RecordHolder record = (RecordHolder) view.getTag();
        ids.add(record.id);
    }
    LocalLogger.LOGGER.info("Just after pre execute");
    super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
    if (params[0].equals("Images")) {
        for (int j = 0; j < count; j++) {
            position=j;
            Bitmap thumbnail;
            try {
                thumbnail = MediaStore.Images.Thumbnails.getThumbnail(
                        activity.getContentResolver(), ids.get(j),
                        MediaStore.Images.Thumbnails.MINI_KIND, null);
                if(thumbnail!=null){
                    publishProgress(thumbnail);
                }
                LocalLogger.LOGGER.info("No exception.");
            } catch (Exception e) {
                LocalLogger.LOGGER.error("Thumbnail Exception : "+e.getMessage());
                e.printStackTrace();
            }

        }
    }else if(params[0].equals("Videos")){
        for (int j = 0; j < count; j++) {
            position=j;
            try {
                Bitmap thumbnail = MediaStore.Video.Thumbnails.getThumbnail(
                        activity.getContentResolver(), ids.get(j),
                        MediaStore.Video.Thumbnails.MINI_KIND, null);
                if(thumbnail!=null){
                    publishProgress(thumbnail);
                }
                LocalLogger.LOGGER.info("No exception.");
            } catch (Exception e) {
                LocalLogger.LOGGER.error("Thumbnail Exception : "+e.getMessage());
                e.printStackTrace();
            }
        }
    }
    return "Done";
}

@Override
protected void onProgressUpdate(Bitmap... values) {
    if(values[0]!=null){
        LocalLogger.LOGGER.info("Got an image");
    }
    view = myGridView.getChildAt(position);
    Bitmap thumbnail=values[0];
    RecordHolder record = (RecordHolder) view.getTag();
    record.imageItem=(ImageView) view.findViewById(R.id.imageViewThumb);
    if(view.getTag()!=null){
        record.imageItem.setImageBitmap(thumbnail);
        view.invalidate();
        view.setTag(null);
    }
    GridViewAdapter arrayListAdapter = (GridViewAdapter) myGridView
            .getAdapter();
    arrayListAdapter.notifyDataSetChanged();
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result) {
    LocalLogger.LOGGER.info("Thumbnail loading complete result "+result);
    super.onPostExecute(result);
}
}

这是我的适配器代码 的 GridViewAdapter.java

public class GridViewAdapter extends ArrayAdapter<Item> {

@Override
public int getCount() {

    return data.size();
}

Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();

public GridViewAdapter(Context _context, int _resource,
        ArrayList<Item> _data) {
    super(_context, _resource, _data);
    this.context = _context;
    this.layoutResourceId = _resource;
    this.data = _data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View gridItem = null;
    try {
        gridItem = convertView;
        RecordHolder holder = null;
        if (gridItem == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            gridItem = inflater.inflate(layoutResourceId, parent, false);
            holder = new RecordHolder();
            holder.imageItem = (ImageView) gridItem
                    .findViewById(R.id.imageViewThumb);
            gridItem.setTag(holder);
        } else {
            holder = (RecordHolder) gridItem.getTag();
            if(holder==null){
                LocalLogger.LOGGER.error("Holder is null");
            }
        }
        Item item = data.get(position);
        holder.imageItem.setImageBitmap(item.getImageBitmap());
        holder.uri = item.getUri();
        holder.id = item.getId();
        notifyDataSetChanged();
        gridItem.setOnTouchListener(new MyOnTouchListener());
    } catch (Exception e) {
        LocalLogger.LOGGER.error("GridViewAdapter: getView: "+e.getMessage());
        e.printStackTrace();
    }

    return gridItem;
}

@Override
public Item getItem(int position) {
    return data.get(position);
}

public class RecordHolder {

    public Uri uri;
    public ImageView imageItem;
    public int id;
}


}

0 个答案:

没有答案