Answer the question
In order to leave comments, you need to log in
How to get an array of links to photo VK API?
I'm trying to get an array of photos from VK and display them in a GridView. Before that, I displayed images in the GridView using an array of links (imageResIds) specified manually. Now I want to get an array of links from VKRequest, but the imageResIds array is always empty. Tell me how to do it right.
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;
}
}
}
Answer the question
In order to leave comments, you need to log in
Figured it out myself. It was necessary to rewrite the imageRequest.executeWithListener method as follows:
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++;
}
mAdapter = new ImageAdapter(getActivity());
final GridView mGridView = (GridView) layout.findViewById(R.id.image_grid);
mGridView.setAdapter(mAdapter);
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question