Android Volley Singleton和自定义gridview适配器

时间:2018-11-14 03:27:51

标签: java android singleton android-volley android-gridview

我正在使用自定义<div role="region" id="announce" aria-live="polite"> 适配器来显示带有凌空单例的图像。但是,当我运行它时,它会返回 onErrorResponse 。看来适配器甚至没有正确设置,这与我在哪里设置适配器有关?我曾尝试在gridview中进行设置,但存在相同的错误。

适配器类看起来不错,所以我认为问题出在onCreate

MainActivity.java:

MainActivity

适配器类:

public class MainActivity extends AppCompatActivity {

private final int THUMBNAIL_SIZE = 250;
private final String KEY = "IMAGE";
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
private static final String URLS[] = {
    "http://10.0.2.2/Android%20A4_1/germany.png",
    "http://10.0.2.2/Android%20A4_1/indonesia.png",
    "http://10.0.2.2/Android%20A4_1/japan.png",
    "http://10.0.2.2/Android%20A4_1/sarawak.png",
    "http://10.0.2.2/Android%20A4_1/singapore.png",
    "http://10.0.2.2/Android%20A4_1/switzerland.png",
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    for(int i = 0; i<URLS.length; i++) {
        LoadingImages(URLS[i]);
    }
}

private void LoadingImages(String url) {
    ImageLoader imageLoader = MySingleton.getInstance(getApplicationContext()).getImageLoader();
    imageLoader.get(url, new ImageLoader.ImageListener() {
        @Override
        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
            Bitmap image = response.getBitmap();
            Bitmap thumbnail = ThumbnailUtils.extractThumbnail(image, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
            bitmaps.add(thumbnail);

            GridView gridView = (GridView)findViewById(R.id.grid_layout);

            final ImageAdapter imageAdapter = new ImageAdapter(MainActivity.this, bitmaps);
            gridView.setAdapter(imageAdapter);

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
                    intent.putExtra(KEY, URLS[i]);
                    startActivity(intent);
                }
            });
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("error", error.getMessage());
        }
    });
}
}

我的单身人士:

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Bitmap> bitmaps;

public ImageAdapter(Context context, ArrayList<Bitmap> bitmaps) {
    this.mContext = context;
    this.bitmaps = bitmaps;
}

@Override
public int getCount() {
    return 0;
}

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

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {


    if (view == null) {
      final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
      view = layoutInflater.inflate(R.layout.griditems, null);
      MyViewHolder myViewHolder = new MyViewHolder(view);
      view.setTag(myViewHolder);
    }
    MyViewHolder myViewHolder = (MyViewHolder)view.getTag();
    myViewHolder.img.setImageBitmap(bitmaps.get(i));

    return view;
}

public class MyViewHolder {
   public ImageView img;

   public MyViewHolder(View view) {
       img = view.findViewById(R.id.image1);
   }
}
}

Main2Activity:

public class MySingleton {
private static MySingleton instance;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private static Context context;

private MySingleton(Context context){
    this.context = context;
    requestQueue = Volley.newRequestQueue(context);
    imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap> cache = new LruCache<>(6);
        @Override
        public Bitmap getBitmap(String url) {
            Bitmap bmp = cache.get(url);
            if (bmp == null) {
                System.out.println("Not in cache");
            } else {
                System.out.println("In cache");
            }

            return bmp;
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            System.out.println("Put in cache");
            cache.put(url, bitmap);
        }
    });
}

public static synchronized MySingleton getInstance(Context context) {
    if (instance == null) {
        instance = new MySingleton(context);
    }
    return instance;
}

public ImageLoader getImageLoader() {
    return imageLoader;
}
}

}

1 个答案:

答案 0 :(得分:3)

更改此方法

@Override
public int getCount() {
return 0;
}

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

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

收件人

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

@Override
public Object getItem(int i) {
return bitmaps.get(i);
}

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