Пытаюсь получить массив photo с VK и вывести их в GridView. До этого выводил изображения в GridView, используя массив ссылок (imageResIds), указанных в ручную. Теперь хочу получить массив ссылок из VKRequest, но массив imageResIds всегда пустой. Подскажите, как сделать правильно.
public class BouquetFragment extends Fragment implements AdapterView.OnItemClickListener {
private ImageAdapter mAdapter;
public String[] imageResIds = new String[10];
public VKPhotoArray vkPhotoArray;
public BouquetFragment() {
// Required empty public constructor
}
private class DownLoadImageTask extends AsyncTask<String,Void,Bitmap> {
ImageView imageView;
public DownLoadImageTask(ImageView imageView){
this.imageView = imageView;
}
//Override this method to perform a computation on a background thread.
protected Bitmap doInBackground(String...urls){
String urlOfImage = urls[0];
Bitmap logo = null;
try{
InputStream is = new URL(urlOfImage).openStream();
//Decode an input stream into a bitmap.
logo = BitmapFactory.decodeStream(is);
}catch(Exception e){ // Catch the download exception
e.printStackTrace();
}
return logo;
}
//Runs on the UI thread after doInBackground(Params...).
protected void onPostExecute(Bitmap result){
imageView.setImageBitmap(result);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new ImageAdapter(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View layout = inflater.inflate(R.layout.fragment_bouquet, container, false);
final GridView mGridView = (GridView) layout.findViewById(R.id.image_grid);
mGridView.setAdapter(mAdapter);
mGridView.setOnItemClickListener(this);
//get the photos from the VK
VKRequest imageRequest = new VKRequest("photos.get", VKParameters.from(
VKApiConst.OWNER_ID, "1",
VKApiConst.ALBUM_ID, "136592355",
VKApiConst.REV, "0",
VKApiConst.EXTENDED, "0",
VKApiConst.PHOTO_SIZES, "0",
VKApiConst.COUNT, "5"),
VKPhotoArray.class);
imageRequest.executeWithListener(new VKRequest.VKRequestListener() {
@Override
public void onComplete(VKResponse response) {
super.onComplete(response);
vkPhotoArray = (VKPhotoArray) response.parsedModel;
int i = 0;
for (VKApiPhoto vkPhoto : vkPhotoArray) {
imageResIds[i] = vkPhoto.photo_130;
i++;
}
}
});
return layout;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
private class ImageAdapter extends BaseAdapter {
private final Context mContext;
public ImageAdapter (Context context) {
super();
mContext = context;
}
@Override
public int getCount() {
return imageResIds.length;
}
@Override
public Object getItem(int position) {
return imageResIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
));
} else {
imageView = (ImageView) convertView;
}
new DownLoadImageTask(imageView).execute(imageResIds[position]);
return imageView;
}
}
}