30

I'd like to get my string-array without extending Activity in my custom class. Is there a way to do this?

String[] foo_array = getResources().getStringArray(R.array.foo_array); will not work without extending Activity, so I need a work-around.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Joseph Webber
  • 2,010
  • 1
  • 19
  • 38

4 Answers4

47

Pass the context to the constructor of custom class and use the same

new CustomClass(ActivityName.this);

Then

Context mContext;
public CustomClass(Context context)
{
    mContext = context;
}

use the context

String[] foo_array = mContext.getResources().getStringArray(R.array.foo_array);

Also keep in mind

Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)

http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html

Also check this

android getResources() from non-Activity class

Edit:

Change this

public class CustomClass(Context context) 
{
}

To

public class CustomClass
{
   Context mContext;
   public CustomClass(Context context) // constructor 
   {
    mContext = context;
   }
}
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • When I change my custom class to `public class CustomClass(Context context) {}` it gives me errors saying `Syntax error on token "(", { expected`. – Joseph Webber Oct 16 '13 at 17:28
  • 1
    @JosephWebber you need to have a constructor not alter the synxtax of class declaration. check the edit – Raghunandan Oct 16 '13 at 17:29
  • @JosephWebber check the edit it should work and also check this http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html – Raghunandan Oct 16 '13 at 17:35
  • 1
    @ Raghunandan Thanks. I misinterpreted what you said, but I remember how to do it now. – Joseph Webber Oct 16 '13 at 17:44
9

try this,

Context context=getApplicationContext();
String[] foo_array = context.getResources().getStringArray(R.array.foo_array);

And, do not use Activity Context as that is tied to the Activity life cycle.

Update,

getApplicationContext() is from Context class. That means any thing extended Context have this method. This also means you will be able to use this from service or from other resources.

But, if you custom class do not extend Activity/context, you have to pass Context as parameter to use getApplicationContext()

if you declare your activity like this

myMethod(Activity activity) //this is bad

Bud if it is like following,

myMethod(Context context) //this is ok

but from above declaration do not pass Activity or Service Context as they have own life cycle. instead you will use getApplicationContext()

minhaz
  • 4,233
  • 3
  • 33
  • 49
  • 1
    `Context context=getApplicationContext();` then gives the error `The method getApplicationContext() is undefined for the type CustomClass`. – Joseph Webber Oct 16 '13 at 16:29
  • why is this `myMethod(Activity activity)` bad? http://developer.android.com/reference/android/content/Context.html. Activity is a known indirect subclass of Context. http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – Raghunandan Oct 16 '13 at 17:22
0

You need pass the Activity context to the Custom class.

private Context context;
public CustomClass(Context context)
{
 this.context=context;
}
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
0

if you use numberpicker and pass String from sring xml then use this

np_Basic_Hight.setMinValue(0);

    np_Basic_Hight.setMaxValue(71);
    np_Basic_Hight.setDisplayedValues(getContext().getResources().getStringArray(R.array.hieght));