java - Android - how to set background image which path is in array -
i'm having following array:
final string[][] dataarr = { {"r.drawable.bengaltiger", "bengaltiger"}, {"r.drawable.cat", "cat"}, {"r.drawable.chimp", "chimp"}, {"r.drawable.eagle", "eagle"}, {"r.drawable.frog", "frog"}, {"r.drawable.lamb", "lamb"}, {"r.drawable.wolf", "wolf"}, };
from 1 i'm trying play sound , use image background button:
final button guessright = (button) findviewbyid(r.id.butright); guessright.setbackgroundresource(r.drawable.bengaltiger);
it's not possible set background image image uri in array because function "setbackgroundresource" expecting int or uri i'm having string.
my question how transform string uri can use path array within "setbackgroundresource" function?
is approach correct? or should use way store data or handle in different way?
thank you.
there number of things wrong here.
- you seem want use string hold symbol name of each of ids. pointed out won't work.
- you storing these in 2-dimensional string array, wrong data structure.
instead, want store these in map.
map<integer, string> imagemap = new hashmap<integer, string> // put other ones here. imagemap.put(r.drawable.bengaltiger, "bengaltiger"); . . . // later on, use them this: for(integer id : imagemap.keyset()) { string name = imagemap.get(id); // can use "id" , "name" in whatever ui elements want. }
also, if storing arbitrary number of ids , names in list, why getting view put them layout? seems want create them programmatically, otherwise why not set in xml layout begin with?
Comments
Post a Comment