0

Mi código no muestra errores pero no funciona lo que necesito, lo que quiero es: que desde la clase romanos se llame el metodo ToDecimal , que se encuentra en la clase RomanTodecimal, y en dicho metodo quiero modificar un textview que se encuentra en la clase romanos, dicho textview se llama numero arabigo y quiero hacerle un settextview(result) la variable result esta definida en el metodo ToDecimal de la clase RomanTodecimal. Alguien puede ayudarme?

Aqui el codigo completo de mis dos clases:

Por cierto estoy trabajando la clase romanos dentro de una section de un tabbed activity, es decir en un fragment.

Clase RomanToDecimal:

import android.os.Bundle;
import android.widget.TextView;
import java.util.HashMap;

/**
 * Created by Johan on 24/04/2018.
 */
public class RomanToDecimal {
    int result = 0;

    public   void toDecimal(String str) {
        int len = str.length();

        /**
         * adding an random char just to be used by the next char to eliminated
         * unnecessary out of index checks
         */
        str = str + " ";
        int result = 0;
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            /** the next char is needed in case the number is using subtractive pattern */
            char nextChar = str.charAt(i + 1);

            /** if ch is M add 1000 - definitely not part of subtractive pattern */
            if (ch == 'M') {
                result += 1000;
                /** if ch is C - possible part of subtractive pattern */
            } else if (ch == 'C') {
                if (nextChar == 'M') {
                    result += 900;
                    /** Additionally increasing the index by 1 as the subtractive pattern was used */
                    i++;
                } else if (nextChar == 'D') {
                    result += 400;
                    /** Additionally increasing the index by 1 as the subtractive pattern was used */
                    i++;
                } else {
                    result += 100;
                }
                /** if ch is D add 500 - definitely not part of subtractive pattern */
            } else if (ch == 'D') {
                result += 500;
                /** if ch is X - possible part of subtractive pattern */
            } else if (ch == 'X') {
                if (nextChar == 'C') {
                    result += 90;
                    /** Additionally increasing the index by 1 as the subtractive pattern was used */
                    i++;
                } else if (nextChar == 'L') {
                    result += 40;
                    /** Additionally increasing the index by 1 as the subtractive pattern was used */
                    i++;
                } else {
                    result += 10;
                }
                /** if ch is L add 50 - definitely not part of subtractive pattern */
            } else if (ch == 'L') {
                result += 50;
                /** if ch is V add 5 - definitely not part of subtractive pattern */
            } else if (ch == 'V') {
                result += 5;
                /** if ch is I - possible part of subtractive pattern */
            } else if (ch == 'I') {
                if (nextChar == 'X') {
                    result += 9;
                    /** Additionally increasing the index by 1 as the subtractive pattern was used */
                    i++;
                } else if (nextChar == 'V') {
                    result += 4;
                    /** Additionally increasing the index by 1 as the subtractive pattern was used */
                    i++;
                } else {
                    result += 1;
                }
            }
        }
        romanos texto=new romanos();
        texto.numeroarabigo.setText(result);
    }
}

Clase romanos:

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class romanos extends Fragment {
    EditText romano,arabigo;
    TextView conversor;
    public TextView numeroromano,numeroarabigo;
    String romanox = "";
    String  numromano,number  ;
    int i,miles, centenas, decenas, unidades,numero,decimal;
    //método para pasar a números romanos
    public  void convertirANumerosRomanos(int numero) {

        //obtenemos cada cifra del número
        miles = numero / 1000;
        centenas = numero / 100 % 10;
        decenas = numero / 10 % 10;
        unidades = numero % 10;



        //millar
        for (i = 1; i <= miles; i++) {
            romanox = romanox + "M";

        }

        //centenas
        if (centenas == 9) {
            romanox = romanox + "CM";

        } else if (centenas >= 5) {
            romanox = romanox + "D";

            for (i = 6; i <= centenas; i++) {
                romanox = romanox + "C";

            }
        } else if (centenas == 4) {
            romanox = romanox + "CD";

        } else {
            for (i = 1; i <= centenas; i++) {
                romanox = romanox + "C";

            }
        }

        //decenas
        if (decenas == 9) {
            romanox = romanox + "XC";

        } else if (decenas >= 5) {
            romanox = romanox + "L";
            for (i = 6; i <= decenas; i++) {
                romanox = romanox + "X";

            }
        } else if (decenas == 4) {
            romanox = romanox + "XL";

        } else {
            for (i = 1; i <= decenas; i++) {
                romanox = romanox + "X";

            }
        }

        //unidades
        if (unidades == 9) {
            romanox = romanox + "IX";

        } else if (unidades >= 5) {
            romanox = romanox + "V";

            for (i = 6; i <= unidades; i++) {
                romanox = romanox + "I";

            }
        } else if (unidades == 4) {
            romanox = romanox + "IV";

        } else {
            for (i = 1; i <= unidades; i++) {
                romanox = romanox + "I";

            }
        }
        romano.setText(romanox);
        numeroromano.setText(romanox);
        romanox="";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_romano, container, false);
        View v1 = inflater.inflate(R.layout.fragment_romano, container, false);
       romano=(EditText)v1.findViewById(R.id.romano);
        arabigo=(EditText)v1.findViewById(R.id.arabigo);
        conversor=(TextView)v1.findViewById(R.id.convers);
        numeroromano=(TextView)v1.findViewById(R.id.numeroromano);
        numeroarabigo=(TextView)v1.findViewById(R.id.numeroarabigo);
        /*try {
            arabigo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) {
                       try {

                           if (Integer.parseInt(arabigo.getText().toString())>3999){
                               Toast toast1 = Toast.makeText(getContext(), "No puede ingresar un valor mayor a 3999 ", Toast.LENGTH_SHORT);
                               toast1.setGravity(Gravity.CENTER, 10, 10);
                               toast1.show();
                           }
                           else {

                               convertirANumerosRomanos(Integer.parseInt(arabigo.getText().toString()));
                           }
                       }catch(Exception e){

                           numero=0;
                       }

                    }
                    return false;
                }
            });

        }catch (Exception e){

            numero=0;
        }
*/
        try {
            romano.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) {
                        try {
                        RomanToDecimal adecimal=new RomanToDecimal();
                        adecimal.toDecimal(romano.getText().toString());
                        }catch(Exception e){

                            //numero=0;
                        }

                    }
                    return false;
                }
            });

        }catch (Exception e){

            //numero=0;
        }

        return v1;
    }
}

1 respuesta 1

0

Convierte el método toDecimal en estático y con retorno de un String.

public static String toDecimal(String str)

Elimina estas dos lineas:

romanos texto=new romanos();
texto.numeroarabigo.setText(result);

Sustitúyelas por:

return result;

En el Fragment Romanos, en el método onCreateView haz:

numeroarabigo.setText(RomanToDecimal.toDecimal());
3
  • gracias amigo, lo probare y te comentare si me funcionó el 27 abr. 2018 a las 4:03
  • Gonzáñez Fernandez pero result no es string sino int y no me deja convertirlo a String el 27 abr. 2018 a las 20:40
  • Gracias , a la final me funciono, aunque cambie el metodo toDecimal pero con tu idea pude llegar a la solución el 28 abr. 2018 a las 5:08

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.