开发者

How to match efficiently result String with various options?

When you have a list activity and in onListItemClick() you need to obtain the selected item and then match it against various options, what is the efficient way to do it? switch case cannot be used s开发者_运维百科ince I want to match Strings. Is a very long if-else if ladder the only way to do it?

Basically the question is how to do something that normally would be done in switch statement with Strings.


In Android it's advised to use an adapter or cursor, and you will use the position parameter to jump in the adapter or cursor to the given position.

Your adapter can be simple as an array, a database cursor, or a custom adapter too.


I would typically try to create enums like Andreas_D posted but if that does not work you can fall back to a Map keyed on your strings with values pointing to some kind of interface or base type.

Something that roughly looks like this using the "Command" pattern.

EDIT: As requested here is an expanded example. This structure will net you the same overall characteristics of a switch statement keyed off a string. Of course the DoActionThing interface could be replaced by a base class.

public interface DoActionThing
{
    public void doSomething();
}

public class Sample
{
    private final String LIST_ITEM_1 = "Foo";
    private final String LIST_ITEM_2 = "Bar";

    private Map<String, DoActionThing> actions;

    public Sample()
    {
        actions = new HashMap<String, DoActionThing>();
        actions.put(LIST_ITEM_1, new DoActionThing()
        {
            @Override
            public void doSomething()
            {
                handleFooSelection();
            }
        });
        actions.put(LIST_ITEM_2,  new DoActionThing()
        {
            @Override
            public void doSomething()
            {
                handleBarSelection();
            }
        });
    }

    public void onListItemClick()
    {
    //Get the selected string from list
        String selected = ....
        DoActionThing dat = actions.get(selected);
        dat.doSomething();
    }
}


You still can use switch with enums - you just have to convert a String input into an enum - or null, if you don't have an enum for it. Like this:

public enum Input {ONE, TWO, THREE, NULL}

and in the code

String selection = getInput();   // some internal magic to get the selectes list item
Input input = Enum.valueOf(Input.class, selection.toUpperCase().trim());
if (input == null) input = Input.NULL;
switch(input) {
  case ONE:   // "one" was selected
  case TWO:   // "two" was selected
  case THREE: // "three" was selected
  default:    // unrecognized / unhandled input
}

Please note, that I skipped the break statements to keep the example short.


Just learned, that if size and/or speed matters enums should be avoided in android development. So the pattern will work with old fashioned static consts too, just change

Input input = Enum.valueOf(Input.class, selection.toUpperCase().trim());

to

int input = decode(selection.toUpperCase().trim());

and make sure, you have the static fields ONE, TWO, ... set and a decoder implementation to convert a String input to one of your constants. Yes, it's much uglier, especially in the decoder, but that one can be hidden somewhere out of sight.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜