gpt4 book ai didi

java - 无法从适配器中的静态上下文引用非静态变量

转载 作者:行者123 更新时间:2023-12-01 11:33:12 27 4
gpt4 key购买 nike

我正在使用基本适配器在 GridView 中显示图像。当图像直接固定 IMAGE_URLS 时工作正常,但我尝试从列表中获取 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;
}
}

最佳答案

将类从私有(private)静态更改为“私有(private)类 ImageAdapter”。

关于java - 无法从适配器中的静态上下文引用非静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30256970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com