2011-03-28 26 views
0

請大家先看看我的代碼:放ImageView的SRC來的HashMap <String,字符串>

private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 

//... 
ImageView temp = new ImageView(MyApp.this); 
temp.setImageResource(R.drawable.icon); 
//... 
HashMap<String, String> map = list.get(i); 
map.put("pic", temp.toString()); //"pic" will map to a ImageView 

這裏是我的questione:

我知道,

map.put("pic", temp.toString()); 

是錯誤。

但是什麼是正確的方法。

在此先感謝。

+0

要提取牛逼究竟是什麼這裏?您將無法僅提取「Drawable」的資源引用。 – 2011-03-28 08:15:13

+0

所以,我不能以這種方式設置ImageView? – cht 2011-03-28 08:25:09

+0

我想從服務器設置ImageView並希望它在任務中執行。所以,我需要知道如何更改ImageView。 – cht 2011-03-28 08:26:56

回答

0

在哈希映射中包含圖像標識符的正確方法是使用HashMap,其中對象對應於圖像ids(int)。請嘗試一下,我自己也在努力。

0

您無法獲得ImageView的資源參考。您只能通過致電getDrawable()ImageView上獲得Drawable

它不會幫助您獲取該Drawable的字符串表示形式。

+0

有沒有辦法可以更改ImageView? – cht 2011-03-28 08:28:39

0

首先得到您的圖標或圖像的可繪製的ID與此。

int idImage = getResources().getIdentifier("R.drawable.icon", "drawable", getActivity().getPackageName()); 

使用getActivity()呼叫getPackageName()之前,如果您使用的片段。

然後傳遞到idImageString把在你的HashMap

String idImageString = String.valueOf(idImage); 

HashMap<String, String> map = new HashMap<String, String>(); 

map.put("imageList", idImageString); 
productsList.add(map); 
0

如果你想顯示從繪製文件夾圖像到HashMap中,你可以很容易做到這一點...您可以顯示也顯示它們成圖像視圖或任何..

HashMap<String,Integer> file_maps = new HashMap<String, Integer>(); 
file_maps.put("Picture1",R.drawable.pic1); 
file_maps.put("Picture2",R.drawable.pic2); 
for(String name : file_maps.keySet()){ 
     TextSliderView textSliderView = new TextSliderView(this); 
     // initialize a SliderLayout 
     textSliderView 
       .description(name) 
       .image(file_maps.get(name)) 
       .setScaleType(BaseSliderView.ScaleType.Fit) 
       .setOnSliderClickListener(this); 

     //add your extra information 
     textSliderView.bundle(new Bundle()); 
     textSliderView.getBundle() 
        .putString("extra",name); 

     mDemoSlider.addSlider(textSliderView); 
    } 

最主要的是<字符串,整數>

相關問題