2011-08-05 51 views
0

我从服务器上的文本文档中检索url字符串。如何为正在点击的图库图片设置网址?

一旦图像被恢复,它们将被设置为使用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); 

现在的问题是如何静态地为每个图像设置一个URL。这意味着当图像发生变化时,网址会发生变化。我怎么可能能够控制哪些网址设置为每个图像?

例如。当图像被点击时,它会打开网页浏览器,为该特定图像设置一个URL。

一周后图像改变了,我怎么能改变与它相关的网址呢?

编辑:当我使用arrayList的字符串时得到的错误。

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) 

这里的错误指向我......

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; 

回答

1

创建一个String对象第二的ArrayList(或链接,如果你有一个类),每个图像将通过你的适配器表示自己在这阵。

例如:

图片适配器包含[Image0,IMAGE1,图像2,图像3,图像4], 虽然与链接您ArrayList包含[Link0,链接1,链路2,链接3,链路4]。

所以,每当有人点击图像,你抓住'位置',并使用它来让你的链接出ArrayList。

希望这是对您有所帮助

+0

那么我怎么能改变字符串对象的arraylist中的URLS?当图像改变时? –

+0

这样做时我得到了ArrayoutOfIndex错误。查看我的编辑 –

+0

我看不出如何做到这一点错误,您在Image Adapter中添加一个对象,同时向ArrayList添加一个URL。这样,你的ArrayList将等于适配器的长度。更改图像时,如果所有图像都更改,则更改某个位置的图像,创建新的阵列列表,如果不更改,则只更改图像已更改位置上的URL。 – Androider

0

你需要一个ArrayList<String>。并且每个图像都将表示在该阵列中。

相关问题