get Selected Operator From Spinner - Android User Interface

Android examples for User Interface:Spinner

Description

get Selected Operator From Spinner

Demo Code

/**/*w  w w .j  ava  2s  . c o m*/
 * Copyright (C) 2014  Marius MOULIS <moulis.marius@gmail.com>
 * 
 * This file is part of MQTT Client.
 *
 *   MQTT Client is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   MQTT Client is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with MQTT Client.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
//package com.java2s;
import android.widget.Spinner;

public class Main {
    public static final String getSelectedOperatorFromSpinner(
            Spinner spinner) {
        String rawOperator = spinner.getSelectedItem().toString();
        String operator = (isNullOrEmpty(rawOperator)) ? "+" : rawOperator
                .replaceAll("\\s|\\w|\\(|\\)", "");

        if (!"+".equals(operator) && !"-".equals(operator)
                && !"/".equals(operator) && !"*".equals(operator)) {
            operator = "+";
        }

        return operator;
    }

    public static final boolean isNullOrEmpty(String stg) {
        return (stg == null || stg.isEmpty() || "".equals(stg.trim()
                .replaceAll("\\s", "")));
    }
}

Related Tutorials