2011-08-05 12 views
0

私は、サーバー上のテキスト文書からURL文字列を収集しています。ギャラリー画像の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が変化することを意味します。どのように私はおそらく各画像に設定されているURLを制御できるようになることができますか?

たとえば、画像がクリックされると、その特定の画像に設定されたURLにWebブラウザが開きます。

イメージが変更されました。どうすれば関連するURLも変更できますか?

EDIT:文字列の配列リストを使用するとエラーが発生します。

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を作成し、それぞれの画像は、お使いのアダプタを介してその配列自体を表します。例えば

:リンクを使用してArrayListのは、[LINK0、リンク1、リンク2、LINK3、LINK4]含まれていますが

イメージ・アダプタは[Image0、画像1、画像2、画像3、のimage4]、 が含まれています。

だから誰かがイメージをタップするたびに、あなたは「位置」をつかんで、それを使ってあなたのリンクをArrayListの外に出します。

・ホープこれはあなたがArrayList<String>を必要とします

+0

どのように私は文字列オブジェクトのarraylistのURLを変更することができますか?画像が変わるとき? –

+0

これを行う際にArrayoutOfIndexエラーが発生しました。私の編集を参照してください –

+0

これは間違って行うことができません表示されません、イメージアダプタでオブジェクトを追加すると同時に、ArrayListにURLを追加します。このようにして、ArrayListはアダプタと同じ長さになります。画像を変更するときは、特定の位置で画像を変更します。すべての画像が変更された場合は新しいarraylistを作成し、そうでない場合は画像が変更された位置のURLのみを変更します。 – Androider

0

に役立ちました。各イメージはその配列内に自身を表します。

関連する問題