0

I can't seem to find a good answer here or elsewhere for this, so here goes. I created a Master/Detail android application, so there are two packages is src:

com.example.app
ItemDetailActivity.java
ItemDetailFragment.java
ItemListActivity.java
ItemListFragment.java

com.example.app.content
ItemContent.java

Now, what I want to do is add a formatted string from a resource in src/res/values/string.xml to the ItemContent.java file. The strings.xml file already contains the contact_information string. When I use this line:

public CharSequence contact_information = getResources().getText(R.string.contact_information);

I get the Eclipse error that The method getResources() is undefined for the type ItemContent. When I use the same line in say, ItemDetailFragment.java it's fine. What am I missing in the content package to allow me to reference the string?

Brian
  • 14,610
  • 7
  • 35
  • 43
ahetman
  • 1,274
  • 11
  • 17
  • Also, rather than using `getResources().getText(int resourceId)` you can just use `getString(int resourceId)` which is a convenience method to avoid the long-winded one you're using – SDJMcHattie Feb 06 '14 at 22:28
  • @SDJMcHattie, would your method still preserve all of my formatting though? That part is crucial to my project. – ahetman Feb 06 '14 at 23:04
  • What formatting do you mean? Are you talking about %s and %d that will have a string and a decimal value substituted like `String.format()`? If so, you can use `getString()` on a `Context` object and you can pass the substitutions in as well all in one, like: `getString(R.string.contact_information, "First name", "Last name")` where `R.string.contact_information` is a `String` of `"Dear %s %s"` – SDJMcHattie Feb 06 '14 at 23:19
  • @SDJMcHattie I care about HTML elements like – ahetman Feb 07 '14 at 17:14

1 Answers1

2

The getResources() method is a Context method: http://developer.android.com/reference/android/content/Context.html

If ItemContent is not extending Context or one of its subclasses, it will not have the getResources() method. One way of getting around this is passing a context reference to your ItemContent or calls made to it that require the context.

Edit: Or, as @desseim suggests in the comments, pass in the Resources object obtained via getResources() to the method that requires it to avoid the possibility of unintentionally leaking the Context.

A couple of examples:

// If you need the context for other things. If you don't keep the Context around as a 
// class or instance variable, there should be no leaks.
public void foo(Context context){
    Resources res = context.getResources();
    // other code
}

Call it from your Activity as:

ItemContent itemContent = new ItemContent();
itemContent.foo(SomeActivity.this);

Or, with resources:

public void bar(Resources res){
    String str = res.getString(R.somestring);
}

Call it from your Activity as:

ItemContent itemContent = new ItemContent();
itemContent.bar(getResources());
Catherine
  • 13,588
  • 9
  • 39
  • 60
  • 1
    Or even better, pass it directly a `Resources` object (obtained through `getResources()` by the caller for example). It avoids unnecessarily leaking a `Context`. – desseim Feb 06 '14 at 22:03
  • Thank you for your help so far, it would be optimal to avoid context leaks, but how exactly would I go about doing this direct Resource passing? Could you provide a clearer example? – ahetman Feb 06 '14 at 23:07
  • Added some clarification. – Catherine Feb 06 '14 at 23:14
  • After adding the bar method from you example to my ItemContent.java file, the code inside of which is: String str = bar.getString(R.string.contact_information); The contact_information is throwing an error stating that "contact_information cannot be resolved or is not a field." I may have misunderstood your example, really shows how new I am to android! – ahetman Feb 07 '14 at 17:44
  • The goal is to access my res/values/strings.xml file in ItemContent so that I can use the contact_information string. – ahetman Feb 07 '14 at 18:03
  • If you added the bar method from my example, the code inside it should be `String str = res.getString(R.string.contact_information);` and you should be calling bar(getResources()). Have you defined the contact_information string in the strings.xml resource file? – Catherine Feb 07 '14 at 22:47
  • Yes, here is the line from strings.xml: `TODO: Contact Information` I have the method exactly as you do: `public void bar(Resources res){ String str = res.getString(R.string.contact_information); }` it just isn't seeing that string resource at all. If I call the same string in say `ItemDetailFragment` from `com.example.app` package it works, just not in the `ItemContent` inside the `com.example.app.content` package. That was why I think it has to do with the packages or something. – ahetman Feb 08 '14 at 16:45
  • It shouldn't be a packages issue if you're importing R properly. Try cleaning your project, too. – Catherine Feb 10 '14 at 21:52
  • Cleaning doesn't help, still can't get this to work. I recently bought an Android book, so if no one can come up with a solution I should be able to figure it out on my own eventually. What is the proper way to import R in this case? I have `import andoid.R;` which throws a warning that I should use the fully qualified version. – ahetman Feb 12 '14 at 21:32
  • There's your problem. You don't want `android.R`, you want your own `R` to be imported. Something like `com.yourpackagename.R`. – Catherine Feb 13 '14 at 00:42