1

¿Se puede poner un String o un Textview que contiene datos tipo 0,8,7,9 en un array tipo Number? Lo quiero para que lo convierta en una estadística.

Éste es mi código:

public class Estadistica2 extends AppCompatActivity {

    private XYPlot mySimpleXYPlot;

    Button btnBorrar;

    String guardado = "";
    TextView datos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_estadistica2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        datos = (TextView) findViewById(R.id.textView);

        //PARA COGER DATOS Y BORRAR CON BOTON
        btnBorrar = (Button)findViewById(R.id.btnBorrar);
        final Context context = this;
        final SharedPreferences sharprefs = getSharedPreferences("SP1", context.MODE_PRIVATE);

        //CUANDO INICIA CARGA EL DATO
        SharedPreferences sharpref = getSharedPreferences("SP1", context.MODE_PRIVATE);
        guardado = sharpref.getString("MiDato","0");
        datos.setText(guardado);

        // Inicializamos el objeto XYPlot búscandolo desde el layout:
        mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);

        // Creamos dos arrays de prueba. En el caso real debemos reemplazar
        // estos datos por los que realmente queremos mostrar

        Number[] series1Numbers = {1,6,4};

        // Añadimos Línea Número UNO:
        XYSeries serie1 = new SimpleXYSeries(
                Arrays.asList(series1Numbers),// Array de datos
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Sólo valores verticales
                "Evolucion"); // Nombre de la primera serie

        // Modificamos los colores de la primera serie
        LineAndPointFormatter series1Format = new LineAndPointFormatter
                (Color.rgb(0, 200, 0),Color.rgb(0, 100, 0),Color.CYAN, new PointLabelFormatter());

        // Una vez definida la serie (datos y estilo), la añadimos al panel
        mySimpleXYPlot.addSeries(serie1, series1Format);
3
  • 1
    Hola tutio y bienvenido/a a Spanish Language. Puede serte útil leer Cómo preguntar y pasar por el recorrido de bienvenida para después editar tu pregunta. Así como está no contiene ni el detalle, ni la descripción necesaria para que podamos ayudarte.
    – fedorqui
    el 13 jun. 2017 a las 9:51
  • No se si he entendido bien tu pregunta, ¿quieres convertir un String tipo "0,1,2,3" en un array de Numbers?
    – Lithorell
    el 13 jun. 2017 a las 12:32
  • Si,exactamente,eso es
    – tutio
    el 14 jun. 2017 a las 14:34

2 respuestas 2

1

Un modo de hacerlo es mediante un split del string, con lo cual obtienes un array de strings, recorres el array y obtienes el valor númerico de cada elemento, y almacenas los valores en un nuevo array ya numérico. Por ejemplo:

String string = "1,2,3";
String[] a_string = string.split(",");
int int[];
for (int i=0;i<a_string.length;i++) {
  int[i] = Integer.parseInt(a_string[i];)
}
0

¿Se puede poner un String o un Textview que contiene datos tipo 0,8,7,9 en un array tipo Number?

Sí, una forma es:

1) Obtén el texto del textView o EditText:

String text = textView.getText().toString();

2) Crea un array de strings usando .split(",") :

   String[] numeros = text.split(",");

3) Cada elemento agregalo a tu array de enteros, convirtiéndolo previamente a entero:

int[] ints = new int[numeros.length];
for (int i=0; i < numeros.length; i++) {
    ints[i] = Integer.parseInt(numeros[i]);
}

Aquí tienes una demo online

Tu Respuesta

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

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