put ImageView src to HashMap<String, String>
Please take a look my code first:
private ArrayList<HashMap<String, String>&开发者_StackOverflowgt; 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
Here is my questione:
I know,
map.put("pic", temp.toString());
is wrong.
But what is the correct way.
Thanks in advance.
The correct way to include image ids in hash map is to use HashMap where object corresponds to the image ids (int). Do try this I have worked on it myself.
You can't get the resource reference of an ImageView
. You can only get the Drawable
by calling getDrawable()
on that ImageView
.
It won't help you to get the string representation of that Drawable
.
First get the drawable id of your icon or image with this.
int idImage = getResources().getIdentifier("R.drawable.icon", "drawable", getActivity().getPackageName());
use getActivity()
before call getPackageName()
if you are using fragments.
then pass the idImage
to String
and put that in your HashMap
String idImageString = String.valueOf(idImage);
HashMap<String, String> map = new HashMap<String, String>();
map.put("imageList", idImageString);
productsList.add(map);
If you want to display image from drawable folder into hashmap you can do it very easily...You can display that also display them into image view or whatever..
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);
}
The main thing is < string, integer>
精彩评论