How do I modify a textview (with settext) that is in another class?

0

My code does not show errors but what I need is not working, what I want is: that from the Roman class we call the method ToDecimal , which is in the class RomanTodecimal , and in that method I want to modify a textview found in the roman class, said textview is called arabic number and I want to make a settextview(result) the variable result is defined in the method ToDecimal of class RomanTodecimal . Can someone help me?

Here is the complete code of my two classes:

By the way, I am working the Roman class within a section of a tabbed activity, that is, in a fragment.

RomanToDecimal class:

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);
    }
}

Roman class:

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;
    }
}
    
asked by Johan Berrio 24.04.2018 в 23:33
source

1 answer

0

Convert the toDecimal method to static and return a String.

public static String toDecimal(String str)

Remove these two lines:

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

Replace them with:

return result;

In the Romans Fragment, in the onCreateView method, do:

numeroarabigo.setText(RomanToDecimal.toDecimal());
    
answered by 26.04.2018 / 12:29
source