I have an SQL database, from the DB I will be getting name string using that I need to show the icon in List.
To do this I followed two methods, one is:
int id= context.getResources().getIdentifier(path + i.getIcon(),null, null);
I found this method is not much efficient, so I followed some other technique i.e
try {
Class res = R.drawable.class;
Field field = res.getField(i.getIcon());
drawableId = field.getInt(null);
holder.ContactImage.setImageResource(drawableId);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
holder.ContactImage.setImageResource(R.drawable.ic_all_contacts_green);
}
Here the problem is, some time the icon name what I get from DB will not be present in Drawable folder, in that time I feel my UI is sluggish if I try to scroll, I tried using both technique mentioned above but I found no use.
How to overcome this problem, when icon is missing first method throws NULL and the second one shows exception, but why does it take too much time in doing that?
how to prevent the sluggishness in my UI, any efficient techniques?
To do this i followed two methods, one is
I would recommend:
context.getResources().getIdentifier(i.getIcon(), "drawable", context.getPackageName())
when icon is missing first method throws NULL and the second one shows exception
Do not ask for an icon that does not exist. Since you know, at compile time, exactly what icons do and do not exist, you are perfectly capable of not asking for icons that do not exist.
how to prevent the sluggishness in my UI
Use Traceview to find out exactly where your performance issue resides. Some will be in reflection -- whether you do it explicitly or (better) use getIdentifier(), there is still reflection going on. Ideally, you would cache the results of the getIdentifier() calls. But beyond that, Traceview will help pinpoint your difficulties.
Related
Of course I know there are yellow warnings that they are important. For example if we use the following code we will get a yellow warning, because we did not use the show() method.
Toast.makeText(getApplication(),"Warning",Toast.LENGTH_SHORT);
But also there are yellow warnings that I do not know they are important or not. For example if I create a button in visual Editor in the android studio, the android studio says to me that I should use the String resources for naming the button.
If I do not use the string resources for naming the button does it make disruption in the program?
The second case:
When I want to use the try{} catch{} in my code and if I do not use a code in the block of the catch, as the result the android studio says to me that the block of the catch is empty. Is filling the block of the catch really important?
For the first part of the question: it does not disrupt your program. Hold your mousecursor on the yellow line for approx. a second and a pop-up appears with the explanation. From the top of my head, I think Android Studio warns you, as you are hard coding text that will be shown to the user. When you use string resources instead, it is really easy to translate the displayed text.
You can read more about String resources here: http://developer.android.com/guide/topics/resources/string-resource.html
For the second part, it sounds like you need to work on your programming skills (not to be rude, but I consider that as one of the basic things in programming land). You wrap code in a try/catch block, as you expect that your code inside the try block might throw an exception. So, if your program ends up in a catch block, you probably want to let the user know in some way, or at least log something, so you know what is going wrong. Read more about catch blocks here: http://developer.android.com/guide/topics/resources/string-resource.html.
I want to know is there any method or any link or tutorial to perform redo undo operation in Android edittext. If any one knows than please let me know.
Quick note on the Antti-Brax/Divers(Kidinov) solution. It works great, except if you try to use it with a TextView post-API 23, you'll run into problems, because guess-what, Google actually added a hidden UndoManager (android.content.UndoManager) and didn't document it or make it obvious it was there. But if you have a hard/bluetooth keyboard in Marshmallow or Nougat and hit ^Z or SHIFT-^Z, you'll get undo/redo.
The problem comes if you're already using Antti-Brax's class with an EditText, and you also hook it to ^Z and shift-^Z, you'll run into problems with anyone using a hard keyboard. Namely the ^Z will trigger BOTH the native and Antti-Brax's undo, leading to two undos simultaneously, which isn't good. And after a few of them, you'll probably get a Spannable out of bounds crash.
A possible solution I found is to subclass the TextView/TextEdit/whatever and intercept the undo/redo calls from the TextView so they don't run as follows:
#Override
public boolean onTextContextMenuItem(int id) {
int ID_UNDO, ID_REDO;
try {
ID_UNDO = android.R.id.undo;
ID_REDO = android.R.id.redo;
} catch (Resources.NotFoundException e) {
ID_UNDO = 16908338; // 0x1020032
ID_REDO = 16908339; // 0x1020033
}
return !((id == ID_UNDO) || (id == ID_REDO)) && super.onTextContextMenuItem(id);
}
Those magic id numbers were found here, and are used only as a backup if the android.R.id.undo values aren't found. (it also might be reasonable to assume that if the values aren't there the feature isn't there, but anyway...)
This is not the best solution because both undo trackers are still there and both are running in the background. But at least you won't trigger both of them simultaneously with ^Z. It's the best I could think to do until this gets officially documented and the getUndoManager() methods of TextView is no longer hidden...
Why they made a feature you can't turn off (or even know if it was there or not) "live" in released Android I can't say.
I just opened an issue on Android's issue tracker if anyone wants to follow this.
There is an implementation of undo/redo for Android EditText in
http://credentiality-android-scripting.googlecode.com/hg/android/ScriptingLayerForAndroid/src/com/googlecode/android_scripting/activity/ScriptEditor.java
The code works but does not handle configuration changes properly. I am working on a fix and will post here when it is complete.
My Google search was :-
android edittext onTextChanged undo
I know this is an old question, but as there is no accepted answer, and this is an issue I've tackled myself from many angles, I'd like to add my solution in case it helps anyone. My answer is probably most relevant to large (1,000words+) volumes of text editing apps that require this feature.
The simplest way to resolve this problem is to make periodic copies of all text on screen, save it to an array and call setText() every time the Undo method is called. This makes for a reliable system, but it isn't ideal for large (i.e. 1,000words+) text editing apps. This is because it:
Is wasteful. It could be that only one word changes in a two thousand word document, so that's one thousand, nine hundred and ninety nine words needlessly committed to memory.
Can lead to performance issues, as some low-tier hardware struggles with rendering large amounts of text. On some of my test devices, this method can lead to freezes of a few seconds whenever Undo is called.
The solution I currently use is comparatively complex, but I've published the results in a library here.
Essentially, this library saves a copy of text as soon as a user begins typing, and then another copy of text once they've stopped typing for a set amount of time (in my case, two seconds). The two text strings are then compared, and the altered section of text returned, the indexes where the alterations occured, and details on whether or not the change was an addition of new text, a deletion, or a replacement of old text with new text.
The net result is that only the necessary text is saved, and when Undo is called, there is only a local delete(), replace() or insert() call, which makes for much faster operations on large text fields.
Here is the undo/redo implementation that was linked to from Gary Phillips' answer extracted into a reusable and universal undo/redo plugin for any widget that descends from a TextView. I added some code for persisting the undo history.
http://code.google.com/p/android/issues/detail?id=6458#c123
Hope this helps.
To preserve EditText Styling with regards to undo:
You can create an ArrayList<EditText> or ArrayList<String> (String containing html text) to store your last 10 (for example) actions. So ArrayList [0] would contain html text from your first action and ArrayList [9] would contain html text from your very last action. Each time the user taps "undo" in your app, you would apply ArrayList [size()-1] to your EditText and then remove ArrayList [size()-1] from your Array.
Thanks for looking.
I'm trying to put together a simple live wallpaper, and trying different methods for optimizing the speed of it. I'm bumbling around a bit, though, so forgive the simpleness of the question.
I'm trying to grab resourceIds by using (this code runs in the class CubeEngine(), which as you can guess I'm just working straight off the Cube demo)
private Resources res;
private int[] resID;
resID[0] = res.getIdentifier("n01","drawable",getPackageName());
Now I have 11 images I would like to load, and so I have 10 of that final line there. I know I could loop it, but I wanted to make it as simple as possible for the first go-round. The problem is that this returns a NullPointerException on execution. The image(s) in question are in res/drawable, and I have had no trouble accessing them before directly, as in
resBMP = BitmapFactory.decodeResources(res, R.drawable.n01);
for example. I'm guessing there is some simple thing that I am missing to get this working. I spend just about every day in MATLAB but haven't worked with a language like java in many years, so even if it's something as simple as syntactical error don't overlook it and don't hesistate to tell me of it!
Ernest's comment solved the problem immediately.
Did you allocate resID (resID = new int[10]) somewhere? If the NullPointerException is on that line, then either resID or res is null.
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 )
I have a long series of graphics -- icon1_0.png, icon1_1.png, icon1_2.png..., icon12_0.png, icon12_1.png, icon12_2.png -- and I'd like to package them with my android application. Ideally I think I should be able to load them as resources but the resource id's are set up as java identifiers. Of course, java identifiers can't be assembled at runtime. I have to ask for R.drawable.icon12_00 so I cannot set up a loop
for(int icon=0;icon<12;icon++)
for(int frame=0;frame<3;frame++)
//syntax error obviously
BitmapFactory.decodeResource(getResources(), R.drawable."icon" + icon + "_" + frame + ".png");
So is there any way to get resources by their names? Better yet, is there a canonical way outside the resource system to pack data files into an android application package so that I can get at them?
I'm thinking about reflection but that doesn't seem like the right solution to me.
Use getResources().getIdentifier() from your Context (e.g., Activity), but please cache the result if you will use it more than once. getIdentifier() is implemented on Resources.
I know you've found an answer already, but if you use reflection then you will see a good speed increase, as getIdentifier() is slower. I wrote about how to do the reflection method here. However, this only works if you're accessing your own resources.
Reflection is also very slow, you should just create an array with all of your identifies in it.