开发者

Unable to select children (textview) of tableRow programmatically

开发者 https://www.devze.com 2023-01-23 07:56 出处:网络
I\'m trying to print the value of the TextViews which are inside TableRows, using TableLayout#getChildAt(i).getChildAt(j).

I'm trying to print the value of the TextViews which are inside TableRows, using TableLayout#getChildAt(i).getChildAt(j).

When I try to log it using the above, logcat complains, saying that it's a View object and that it doesn't have the method I'm trying to use (getText()).

The only Views in the TableRows are 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 fi开发者_JAVA技巧rst row's first TextView
    Log.d(TAG, ("row1_tv1: " +
            tblLayout.getChildAt(0).getChildAt(0).getText().toString));

    ...
}


Have you tried something like this?

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

You can also do it in one line, but it sometimes looks ugly:

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

Anyway... what you are doing here is casting the View to the specific type you want. You can do that without problems, since you are completely sure that each TableLayout's child is a TableRow, and you know that TableRow's children on XXX position is a TextView.

0

精彩评论

暂无评论...
验证码 换一张
取 消