无法从Adapter中的静态上下文引用非静态变量

时间:2015-05-15 10:16:53

标签: java android android-arrayadapter

我正在使用Base Adapter在网格视图中显示图像。图像直接修复IMAGE_URLS时工作正常,但我尝试使用列表中的geting url并分配到显示非静态变量的IMAGE_URLS中,无法从静态上下文中引用。我不知道如何解决这个问题。请帮忙解决这个问题

public class ImageGridFragment extends AbsListViewBaseFragment {

 public static final int INDEX = 1;
     String description="";
     ArrayList<String> img = new ArrayList<String>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fr_image_grid, container, false);
    listView = (GridView) rootView.findViewById(R.id.grid);
            img.clear();
        Bundle bundle = this.getArguments();

        description = bundle.getString("description");
        String[] separated= description.split(",");
    for(int i=0;i<separated.length;i++)
    {
        img.add(separated[i]);
    }

    ImageGalleryFragment.imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
    ((GridView) listView).setAdapter(new ImageAdapter(getActivity()));
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startImagePagerActivity(position);
        }
    });
    return rootView;
}




private static class ImageAdapter extends BaseAdapter {

        String[] IMAGE_URLS =  img.toArray(new String[img.size()]);

    private LayoutInflater inflater;

    private DisplayImageOptions options;

    ImageAdapter(Context context) {
        inflater = LayoutInflater.from(context);

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_stub)
                .showImageForEmptyUri(R.drawable.ic_empty)
                .showImageOnFail(R.drawable.ic_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    @Override
    public int getCount() {
        return IMAGE_URLS.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.item_grid_image, parent, false);
            holder = new ViewHolder();
            assert view != null;
            holder.imageView = (ImageView) view.findViewById(R.id.image);
            holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        ImageLoader.getInstance()
                .displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        holder.progressBar.setProgress(0);
                        holder.progressBar.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                        holder.progressBar.setVisibility(View.GONE);
                    }

                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        holder.progressBar.setVisibility(View.GONE);
                    }
                }, new ImageLoadingProgressListener() {
                    @Override
                    public void onProgressUpdate(String imageUri, View view, int current, int total) {
                        holder.progressBar.setProgress(Math.round(100.0f * current / total));
                    }
                });

        return view;
    }
}

static class ViewHolder {
    ImageView imageView;
    ProgressBar progressBar;
    }
}

1 个答案:

答案 0 :(得分:1)

将类从private static更改为“private class ImageAdapter”。