PreferenceManager(context) is private in the package and I need to convert it to public to use. I don't know how to do so. So, can anyone help me ?
First of all, if you have an issue, you should post the actual code that's causing it.
But specifically with PreferenceManager, you can't invoke it the way you posted. If you want to instantiate it, you would need to do something like this:
String mPrefManager = mContext.getPreferenceManager()
where mContext would be the actual Context that you're trying to use
Related
I am new to programming. Can you please explain what the difference between notifyDataSetChanged() and Adapter.this.notifyDataSetChanged() is? I think it has something to do with that context stuff?
I´m wondering which one I should use for my RecyclerView changes.
They are likely one and the same. this is a Java keyword to specify the current object, and the only reason you would have to specify Adapter.this before the method is if you were in an inner class where this would refer to something else.
I'm just starting with programming Android apps and was working through the developers guide on android.com. When trying to display a text on a second activity it says it cannot resolve the symbol EXTRA_MESSAGE as you can see here:
As far as I can tell I did every step like the guide says. I also tried copy and pasting everything but it still doesn't work. What am I missing?
You're doing a static import of extra message from some random class in MainActivity. That's wrong, don't do that. Define EXTRA_MESSAGE as a public final static String, with whatever value you want (I'd suggest "message") in your MainActivity.
There is two ways of solving it.
1) Use same static variable (Its a dirty way). At DisplayMessageActivity use android.provider.AlarmClock.EXTRA_MESSAGE.
2) This approach I recommend you. Create public static final String field at MainActivity, remove android.provider.AlarmClock.EXTRA_MESSAGE, and use MainAcitivity field at both classes. Content of this variable does not matter as long as it is unique extra key.
I need to use Toast.makeText(getApplicationContext()). The getApplicationContext() is what I am really after. I need a specific tutorial on passing Context around. Any sugestions? I see alot of developers answering questions with techo-speak like this and I would seriously like to advance to that stage. I have run into passing Context issues way too often and would like a deep understanding of this. Until I get this type of knowledge I will always consider myself a noob.
Thanks.
Do you have a problem with Activity being a Context ? (Activity.this)??
Android already does that for you so why do you want to bother your head.? When you create a View the Context is passed to the View eg; TextView textv = new TextView(Context); you can later retrieve that Context with View.getContext(). Honestly Context are everywhere so why do you really want to use the getApplicationContext(); why not getBaseContext()
I'm having some problems on where and how to place and call the getString().
I tried at the beginning of my activity where I usually define it but always get an error. Should it be only after onCreate()?
Some of my strings I use it very often so I don't want to use getString() every time I use them.
Is this the correct way?
private String helloWorld = getString(R.string.hello_world);
Thank you very much! :)
at the top of your file do:
private String helloWorld;
And then, in onCreate(),
do:
helloWorld = getString(R.String.hello_world).
The problem is that getString needs a reference to the activity context, which has not been associated with your class until onCreate is called.
Is there a way to have an global settings variable for an android application, which is accessable as well from any help java classes without giving them context?!
I try to explain what I mean.
I have an application version as string value in strings.xml
I can get its value from every android activity, but not from help java classes withought giving context
What I do now, is saving it in a static variable of my first activity, but it seems, that sometimes it will be erased and set to null.
May be I do something wrong?!
Sorry for newbie question.
And thank you in advance,
Mur
P.s.
I wrote a small tutorial for this topic, to show the solution.
A variable declared as public, static, and final will be visible to all of your classes and never get erased.
public static final String VERSION = "1.2.3.4";
You could make a public static variable in your application class that you fill with the value from strings.xml in the onCreate method. The application class is a singleton and will be the last thing that is killed as part of your app so it will always be there and if you make it public static there will be only one instance.
I'm guessing that you have a JAVA class for some common utility functions. You get the value of your string using a context in your Activity/Service and then pass in that value to the JAVA class function as a parameter.