Reputation: 3149
I am using the Picasso library to load images in a GridView. Here is my code.First I have a class that contains String array of links.For experimental purposes I use the same image.
public class Data {
static final String[] URLS = {
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
"http://www.panoramio.com/photo/116726502",
};
private Data() {
// No instances.
}
}
Next I have the MainActivity Class which does the usual stuff,ie initializing the GridView etc.
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gv = (GridView) findViewById(R.id.grid_view);
gv.setAdapter(new SampleGridViewAdapter(this));
gv.setOnScrollListener(new SampleScrollListener(this));
}
}
Finally I have the GridViewAdapter that I created.
final class SampleGridViewAdapter extends BaseAdapter {
private final Context context;
private final List<String> urls = new ArrayList<String>();
public SampleGridViewAdapter(Context context) {
this.context = context;
// Ensure we get a different ordering of images on each run.
Collections.addAll(urls, Data.URLS);
Collections.shuffle(urls);
// Triple up the list.
ArrayList<String> copy = new ArrayList<String>(urls);
urls.addAll(copy);
urls.addAll(copy);
}
@Override public View getView(int position, View convertView, ViewGroup
parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(context);
view.setScaleType(CENTER_CROP);
}
// Get the image URL for the current position.
String url = getItem(position);
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context) //
.load(url) //
.placeholder(R.drawable.placeholder) //
.error(R.drawable.error) //
.fit() //
.tag(context) //
.into(view);
return view;
}
@Override public int getCount() {
return urls.size();
}
@Override public String getItem(int position) {
return urls.get(position);
}
@Override public long getItemId(int position) {
return position;
}
}
Upvotes: 0
Views: 386
Reputation: 3149
Ok. I found some time post the solution to this problem.
public class MainActivity extends Activity {
private GridView gridView;
private GridviewAdapter gridAdapter;
String[] items = {
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
"http://static.panoramio.com/photos/large/116726502.jpg",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridviewAdapter(MainActivity.this, items);
gridView.setAdapter(gridAdapter);
}
}
And an adapter that does the usual stuff.
public class GridviewAdapter extends BaseAdapter{
private Context context;
private String[] items;
public GridviewAdapter(Context context, String[] items){
super();
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int position) {
return items[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = null;
if (convertView == null) {
img = new ImageView(context);
convertView = img;
img.setPadding(5, 5, 5, 5);
} else {
img = (ImageView) convertView;
}
Picasso.with(context)
.load(items[position])
.placeholder(R.drawable.picture)
.resize(200, 200)
.into(img);
return convertView;
}
}
Hope this is going to be useful for someone...
Upvotes: 0
Reputation: 4532
Your URL Array is not correct.. check that and make a valid array of URL. That array may be like..
static final String[] URLS = {
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
"http://www.panoramio.com/photo/116726502.jpg",
};
private Data() {
// No instances.
}
}
Upvotes: 0
Reputation:
Can you please try to change url to real photo url
e.g.
http://static.panoramio.com/photos/large/116726502.jpg
Upvotes: 0
Reputation: 12378
The urls you are passing is not an actual image url, it takes to the webpage.
I inspected and extracted image url for one of the url. Use this it will work. http://static.panoramio.com/photos/large/116726502.jpg
Make sure you have the actual image url while loading the images in the app
Upvotes: 1