Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoCompleteTextView adapter, "hidden" adapter?

I have 3 AutoCompleteTextViews, and I would like to register 2 String[] adapters on them. Currently, I'm doing this:

atw_from.setAdapter(new ArrayAdapter(ctx, android.r.layout.simple_dropdown_item_1line, stages_adapter));

Let's say my user wants to type "Középmező", he starts to type "Közé" and he will be offered to choose Középmező, until this, it is pretty simple. But what if the user is too lazy to type accents (and a lot of them are lazy), thus he will type Kozepmezo only, then he won't get any offer, since there is no Kozepmezo in my String[]. The thing I want is, if he types in "Koze", he should be offered Középmező, so even if he doesn't uses accents, he will be always offered the actual word with the accents.

Currently, I have a pretty silly solution, I have a String[] with double the size of the original [], the first half contains the words with accents, the second contains the deaccented versions. So now, if he types Közé, he will be offered Középmező, and if he types Koze, he will be offered Kozepmezo. It works because the server can process both versions, but it just looks silly, and I want to solve it.

From what I understand, I should make a full custom adapter. Is that the best approach, or is there any solution included in the SDK? If I should make the custom adapter, could anyone point me in the right direction, on how to do that? :)

EDIT: added my own answer, should work for everyone, cheers for the other answer, which directed me to the good direction!

like image 493
hundeva Avatar asked Nov 05 '22 04:11

hundeva


1 Answers

Okey, after a lot of pain in the arse dealing with this, here's the thing I did at the end. This is NOT a good practise at all, and I may do it wrong, but at least it is working perfectly now.

Simply ctrl+c, ctrl+v the source code of BaseAdapter, and ctrl+c, ctrl+v the source code of ArrayAdapter. Now look at the private inner class, ArrayFilter, especially the performFiltering method. Modify(not override!) it, as much as you want, in my case, I added a lot of .replace("x","y") thingies, for the deaccenting part.

Whatever else I tried, either produced unpredictable force closes(a lot, and completely random ones), or I just couldnt do it, since too much methods/variables are private, instead of protected. I must say, Google should revisit theese codes...

Note: you don't really need to ctrl+c ctrl+v the BaseAdapter codes, since it is a public abstract class, but hey, it is not that much of a code, and this way everything is there, clearly visible for you.

cheers

like image 69
hundeva Avatar answered Nov 11 '22 17:11

hundeva