我從服務器上的文本文檔中檢索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;
那麼我怎麼能改變字符串對象的arraylist中的URLS?當圖像改變時? –
這樣做時我得到了ArrayoutOfIndex錯誤。查看我的編輯 –
我看不出如何做到這一點錯誤,您在Image Adapter中添加一個對象,同時向ArrayList添加一個URL。這樣,你的ArrayList將等於適配器的長度。更改圖像時,如果所有圖像都更改,則更改某個位置的圖像,創建新的陣列列表,如果不更改,則只更改圖像已更改位置上的URL。 – Androider