How to set URL for gallery images being clicked?
I am retreiving url strings from a text document on a server.
Once the images are retreived they are set to a gallery using a ImageAdapter.
public class ImageAdapter extends BaseAdapter {
/** The parent context */
private Context myContext;public ImageAdapter() {
// TODO Auto-generated constructor stub
}
/** URL-Strings to some remote images. */
public String[] myRemoteImages = {imageUrl,imageUrl2,imageUrl3,imageUrl4};
private String[] mImageURLs = {
"http://www.google.com",
"http://www.google.com"};
/** Simple Constructor saving the 'parent' context. */
public ImageAdapter(Context c) { this.myContext = c; }
/** Returns the amount of images we have defined. */
public int getCount() {
return this.myRemoteImages.length;
}
/* Use the array-Positions as unique IDs */
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/** Returns a new ImageView to
* be displayed, depending on
* the position passed. */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(this.myContext);
// i.setTag(mImageURLs[position]);
try {
URL aURL = new URL(myRemoteImages[position]);
Log.v("ImageLoader", "Remote images set");
URI imageUri = null;
//Setting the Uri of aURL to imageUri.
try {
imageUri = aURL.toURI();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Testing to see if images are already in cache, if not then we load the images from the web and save them to the cache.
if (new File(new File(myContext.getCacheDir(), "thumbnails"), "" + imageUri.hashCode()).exists())
{
Log.v("Loader", "File exists in cache. Now pulling from the cache");
String cachFile = myContext.getCacheDir() +"/thumbnails/"+imageUri.hashCode();
FileInputStream fis;
try {
fis = new FileInputStream(cachFile);
Bitmap bm = BitmapFactory.decodeStream(fis);
i.setImageBitmap(开发者_如何学编程bm);
Now the problem is how can i set a URL for each image statically. Meaning that the url's will change when the images change. How could i possibly go about being able to control what urls are set to an each image?
For example. when a image is clicked, it opens up web browser to a URL set for that particular image.
A Week later the images change, How could i go about changing the url associated with it also?
EDIT: Error i get when using the arrayList of strings.
08-05 16:56:50.748: ERROR/AndroidRuntime(646): java.lang.ArrayIndexOutOfBoundsException: index=2 length=2
08-05 16:56:50.748: ERROR/AndroidRuntime(646): at com.fttech.gameIT.MainMenu$ImageAdapter.getView(MainMenu.java:379)
08-05 16:56:50.748: ERROR/AndroidRuntime(646): at android.widget.Gallery.makeAndAddView(Gallery.java:748)
08-05 16:56:50.748: ERROR/AndroidRuntime(646): at android.widget.Gallery.fillToGalleryRight(Gallery.java:700)
08-05 16:56:50.748: ERROR/AndroidRuntime(646): at android.widget.Gallery.layout(Gallery.java:631)
08-05 16:56:50.748: ERROR/AndroidRuntime(646): at android.widget.Gallery.onLayout(Gallery.java:339)
08-05 16:56:50.748: ERROR/AndroidRuntime(646): at android.view.View.layout(View.java:9330)
08-05 16:56:50.748: ERROR/AndroidRuntime(646): at android.view.ViewGroup.layout(ViewGroup.java:3795)
The error points me here...
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(this.myContext);
try {
URL aURL = new URL(myRemoteImages[position]);
Log.v("ImageLoader", "Remote images set");
//It points me here i.setTag(mImageURLs[position]);
URI imageUri = null;
Create a second ArrayList with String objects (or links if you have a class), each Image will represent itself in that Array via your Adapter.
For example:
Image Adapter contains [Image0, Image1, Image2, Image3, Image4], While your ArrayList with links contains [Link0, Link1, Link2, Link3, Link4].
So whenever someone taps an image, you grab the 'position', and use it to get your link out of the ArrayList.
Hope this was helpful to you
You need an ArrayList<String>
. and each Image will represent itself in that array.
精彩评论