Is it possible to use a ContentObserver to monitor variable changes (e.g. String) by constructing a Uri and passing it as an argument when registering? If yes, can you provide a functional example?
It won't work by doing Uri.parse on an arbitrary string value.
I don't think it will work at all. What's the use case? It suggests that something outside of your control is modifying a variable. How can this happen?
It should work with string value,
It works with me. I have used Uri.parse like below:
contentResolver.notifyChange(Uri.parse("content://com.example.provider/notification"), null)
Related
When i got some cursor i want to be aware for changes, so i have used the registerContentObserver() on my cursor and when change occur i just notify that change happened.
i looked into the notifyChange method on android developer and i didnt see any way to pass some metadata .
when i said metadata i meant any other object which tell me what change happen like delete/update/insert
You can't specifically add any metadata per se, but you can bend the system to pass this information anyway.
When registering your content observer, set the notifyForDescendants parameter to true. Then in your ContentProvider, generate a different uri to add information.
For example if the uri you normally use is content://com.example.app.provider/item/42, you can use one of the following uris to add information :
content://com.example.app.provider/item/42/inserted
content://com.example.app.provider/item/42/updated
content://com.example.app.provider/item/42/deleted
I have a value saved in SharedPreferences that is a float. I tried to save this as a int after some design changes, but I cam getting a ClassCastException. The exception is thrown when I try to load this value.
The offending line: preferences.getInt(myKey, myDefaultValue)
Which was changed from: preferences.getFloat(myKey, myDefaultValue)`
Is there a safe way to load from this key and overwrite this value without having to clear my cache? Or will I need to create a totally new key?
You should remove the value from preferences first.
Call
Editor.remove(String key);
and then set your value.
It also depends on what you are trying to achieve. If you are trying to store 'int' in settings, and then store there a 'float', and then 'int' again, the answer in NO, NEVER DO LIKE THAT!
If you just changed the variable type during development before any production, then you can just clear the app data and work with new variable without needing to clear it in code.
But if you've already deployed your application and now you want to change the variable type, then yes, you can do it and use .remove() method.
In my case my app is already published and could not changed the old type of SharedPreference I had to surround the call with a try-catch for ClassCastException
It is possible to use another key in this case. It looks safer for me. If you use the same key you have to put Editor.remove(String key); everywhere before putting the new value. And you have to remember that you can't just call getInt() method without wrapping it to try-catch. It may be not obvious for somebody who will edit the code later.
Programming my application would be a lot easier if I could refer to an array by URI, and use that array to inflate a list. Has anyone done this? Thanks in advance!
No, it's not possible to refer to an array using a Uri in the same way that you do using R.array.yourarray.
Technically, just to say the proper answer, you could wrap an object to parse the Uri for you and return the R.array.yourarray for you. Like a Content Provider, for example. But obviously that is just nonsense, there is no reason to create a non standard thing when the standard works just fine.
I don't think there is any scenario that would require a Uri that the standard way doesn't handle just fine.
i found this problem some time ago, but i solve it using this: getString(), or this: getResources().getString()
but now, for this case, it doesn't works, i think it's because i need to get the string values on a NON ANDROID ACTIVITY CLASS. I need the resource values on a remote connection class, that doesn't extends any kind of activity or service.
how i can acces to the variables from my strings.xml on this normal class?
this is the code where i get the error (it gets an integer, and not the string value)
String a =R.string.totalpermission;
Take a look at these two answers (are the same XD):
How to obtain AssetManager without reference to Context?
How can I get a resource content from a static context?
Just an advice: try to read some basic concepts... it seems you don't understand what the R class is and how to use it. Trust me, you waste less time studying than trying to figure out how things work.
I'll add something to existing answers since I found it very useful.
To get your strings you have to use a Context. Your activity will work just great.
String string = getString(R.string.myString);
But if you have something more complex... for exemple
R.string.result -> "You %1$s %2$d cats"
String result = getString(R.string.result, killed ? "killed": "saved", count);
That would give you a result like that:
You saved 10 cats or You killed 2 cats... and so on. You can pass parameters and positional arguments in strings will get replaced by your arguments in getString.
All Android resources are referenced via a resource ID, like R.string.totalpermission. You can see those numbers in R.java (although there's no reason to ever do that).
In cases of strings, you can easily get those using Context.getString. Bonus: You can even pass optional arguments and add dynamic strings that way. You always have a context - how are you getting called? If you really don't have a context, you can create one for the package your resources are in.
hi i have found Uri as immutable reference i dont know what it is the exact meaning of immutable reference... can anyone help me?
It's a variable that cannot be changed once set. Very useful when you have multithreaded code since being able to change a variable's value might be a source of many hard to find problems in your code.
If it's immutable, it's usually good.
A good example of an immutable class within the .NET Framework is System.String. Once you create a String object, you can’t ever change it. There’s no way around it; that’s the way the class is designed. You can create copies, and those copies can be modified forms of the original, but you simply cannot change the original instance for as long as it lives, without resorting to unsafe code. If you understand that, you’re probably starting to get the gist of where I’m going here: For a referencebased object to be passed into a method, such that the client can be guaranteed that it won’t change during the method call, it must itself be immutable.
In a world such as the CLR where objects are held by reference by default, this notion of immutability becomes very important. Let’s suppose that System.String was mutable, and let’s suppose you could write a method such as the following fictitious method:
public void PrintString( string theString )
{
// Assuming following line does not create a new
// instance of String but modifies theString
theString += ": there, I printed it!";
Console.WriteLine( theString );
}
Imagine the callers’ dismay when they get further along in the code that called this method and now their string has this extra stuff appended onto the end of it. That’s what could happen if System. String were mutable. You can see that String’s immutability exists for a reason, and maybe you should consider adding the same capability to your design.
EX: string is immutable...
if u have for ex string s =" whatever" and u output it with uppercase letter..for ex
Console.Write(s.ToUpper())the console will print u WHATEVER...but the string s will still be whatever... unlike the mutable type which will change the string from whatever to WHATEVER
"immutable" means "can't change the value"
"mutable" == "changeable"
"immutable" == "not changeable"
In java , every thing is treated as String and object , Now try to think that if have created a program of 10000 lines and in this there you have added "public" 100 times so do you think that every time this public is created in storage . else what we can do , we can created something like that when ever we find something like this we will fetch it from there there ( String pool )