Pregunta

Estoy tratando de imprimir el valor de los TextViews que están dentro TableRows, utilizando TableLayout#getChildAt(i).getChildAt(j).

Cuando intenta iniciar sesión utilizando el anterior, Logcat se queja, diciendo que es un objeto de Vista y que no tiene el método que estoy tratando de uso (getText()).

Las únicas vistas en las TableRows son TextViews.

// List<TextView> textViewBoxes...

private void createViews() {
    ...

    tblLayout = new TableLayout(this);
    tblRow01 = new TableRow(this);
    ...

    for (int i = 0; i < 99; i++) {
        TextView text = new TextView(this);
        text.setText("Player " + i);
        textViewBoxes.add(text);
    }

    tblRow01.addView(textViewBoxes.get(0));
    ...

    tblLayout.addView(tblRow01);
    ...

    // Print the contents of the first row's first TextView
    Log.d(TAG, ("row1_tv1: " +
            tblLayout.getChildAt(0).getChildAt(0).getText().toString));

    ...
}
¿Fue útil?

Solución

¿Usted ha intentado algo como esto?

TableRow row = (TableRow)tblLayout.getChildAt(0);
TextView textView = (TextView)row.getChildAt(XXX);
// blah blah textView.getText();

También puede hacerlo en una sola línea, pero a veces ve feo:

// wtf?
((TextView)((TableRow)tblLayout.getChildAt(0)).getChildAt(XXX)).getText();

En fin ... lo que está haciendo aquí es echando la vista con el tipo específico que desea. Puede hacerlo sin problemas, ya que está completamente seguro de que cada niño es un TableLayout's TableRow, y usted sabe que los niños TableRow's en posición XXX es un TextView.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top