Re: [android-developers] Re: How do I refer to a drawable with a variable

2010-09-15 Thread { Devdroid }
On 14 September 2010 17:24, nextgen nextgenfant...@comcast.net wrote:
 Thank you for this, it gives me a possible solution.  Unfortunately
 getDrawable will only take an integer as an input, which really
 defeats the simplicity of solution I'm looking for.  I can set about
 assigning integers to each team, but I'm not sure that would even do
 it because I'm not sure about the nature of the integer it wants.
 Here is the description I see in Eclipse:


// returns ID of drawable given by name (i.e. getDrawableByName(
app_icon );
public static int getDrawableByName( String name )
{
int drawableId = 0;

try {
Class res = R.drawable.class;
Field field = res.getField( name );
drawableId = field.getInt(null);
}
catch (Exception e) {
//  Toast.makeText(context, DIT Service: onStartCommand(),
Toast.LENGTH_SHORT).show();
Log.e(DateInTray, Failure to get drawable:  + name, e);
}

return( drawableId );
}

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Re: How do I refer to a drawable with a variable

2010-09-14 Thread Kostya Vasilyev


This:

http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, 
java.lang.String, java.lang.String)


can be used to convert string resource identifier to its numeric value.

This function is not terribly efficient, so you might want to cache the 
result(s).


-- Kostya

14.09.2010 19:48, DanH пишет:

R.drawable.Ind is an integer.

On Sep 14, 10:24 am, nextgennextgenfant...@comcast.net  wrote:

Thank you for this, it gives me a possible solution.  Unfortunately
getDrawable will only take an integer as an input, which really
defeats the simplicity of solution I'm looking for.  I can set about
assigning integers to each team, but I'm not sure that would even do
it because I'm not sure about the nature of the integer it wants.
Here is the description I see in Eclipse:

The desired resource identifier, as generated by the aapt tool. This
integer encodes the package, type, and resource entry. The value 0 is
an invalid identifier.

Unfortunately I don't understand that.  I'm relatively new to android
and java.  Any other thoughts would be greatly appreciated.

Thanks!

On Sep 14, 3:34 am, KANTESH BABANNAVARkantesh...@gmail.com  wrote:


I think it may help you..
getresources().getDrawable(TeamArray[position]);
Where TeamArray[position] = {
R.drawable.drawable1,
R.drawable.drawable2,
.
.
R.drawable.drawableN,
}
These links may help 
you..http://developer.android.com/resources/tutorials/views/hello-gridview...
On Sep 14, 8:49 am, nextgennextgenfant...@comcast.net  wrote:  I am doing an 
app related to football.  I have a list view that will

have various rows representing teams.  In my custom array adapter I
want to write some code that will present an icon for the team
represented by the row.  I have a drawable resource for each team (for
instance for Indianapolis, Ind.png).
In my code I can't refer to R.drawable.Ind because the Ind part is
dynamic.  That value would be in an array, like TeamArray[position].
As I expected, I certainly can't use R.drawable.TeamArray[position]
because it won't even compile, it expects a literal name from the
drawable folder.
Is there a way to do this?



--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en