2012-08-23 64 views
1

這可能是一個我可能再次忽略的問題,但如果你能幫我解決它,我將非常感謝。使用此代碼 擺脫繪製圖像並將其發送到Imageview.setImageResource(在我的ImageView是一個gridview內);一切正常Android使用ArrayList在ImageView上加載圖像

private Integer[] imageIDs = { 
      //R.drawable.image1, 
      R.drawable.image2, 
      R.drawable.image3, 
      R.drawable.image4, 
      R.drawable.image5, 
      R.drawable.image6, 
      R.drawable.Image7, 
}; 

//Here is my another code to call imageIDs as ImageResource 
ImageView iv = (ImageView)v.findViewById(R.id.grid_item_picture); 
iv.setImageResource(imageIDs[position]); 
//position is a default value of gridview starting from 0 

現在我要完成的是刪除數組轉換它僅僅是因爲需要這些內部數據是動態的。所以香港專業教育學院創建這些新代碼

ArrayList<String> array_image = new ArrayList<String>(); 
     array_image.add("R.drawable.image6"); 
     array_image.add("R.drawable.image1"); 

和ImageView的ImageResource設置爲這個代碼

iv.setImageResource(array_image .get(position)); 

我收到一個錯誤說:在

的方法setImageResource(INT)類型的ImageView不適用於參數(對象) 你能幫我解決/找出它嗎?

回答

7

您正在爲字符串定義一個ArrayList。但是,您需要整數作爲資源標識符。

反而這樣做;

ArrayList<Integer> array_image = new ArrayList<Integer>(); 
array_image.add(R.drawable.image6); 
array_image.add(R.drawable.image1); 
+0

我已經嘗試過,先生,但是它仍然是給我一個錯誤說 image6不能得到解決或無法在現場 – Janwel

+0

W8先生生病再試一次我想我再造一個語法錯誤 – Janwel

+0

現在我得到這個錯誤「ImageView類型中的方法setImageResource(int)不適用於我的代碼iv.setImageResource(array_image.get(position))上的參數(Object)」; – Janwel

0

ArrayList<int> array_image = new ArrayList<int>(); 
array_image.add(R.drawable.image6); 
array_image.add(R.drawable.image1); 

嘗試,然後

iv.setImageResource(array_image .get(position)); 
+0

我已經試過,先生,但仍然給我一個錯誤說 image6,image1無法解析或不是一個字段 如果我嘗試添加「」錯誤是現在這個 方法add(Integer)在類型ArrayList 不適用於參數(字符串) – Janwel

+0

請確保它的ArrayList 不是ArrayList 並且您要添加的值不包含「」 –

+0

爲什麼您發佈auselen提供的相同答案?你是否意識到'ArrayList'不能用於原始類型? –

1

嘿@Janwel嘗試使用這一個。這很有可能。

List<Integer> mL = new ArrayList<Integer>(); 
mL.add(R.drawable.image2);