admin管理员组

文章数量:1290982

I have a bunch of text views in a linear layout. The user's interaction with them is view-only: If the user guesses correctly (using voice), that guess will show in one of the text views. It looks like this (focus on the green boxes):

The pertinent XML looks like this:

<ScrollView
    android:id="@+id/wof_guesses"
    ...
    >

    <LinearLayout
        android:id="@+id/wof_guesses_list"
        ...
        android:orientation="vertical">

        <TextView
            android:id="@+id/wof_guess0"
            style="@style/WofGuess" />

        <TextView
            android:id="@+id/wof_guess1"
            style="@style/WofGuess" />

        <TextView
            android:id="@+id/wof_guess2"
            style="@style/WofGuess" />

        <TextView
            android:id="@+id/wof_guess3"
            style="@style/WofGuess" />

        ...

    </LinearLayout>
</ScrollView>

And this is the style XML:

<style name="WofGuess" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/mentor_grey_100</item>
    <item name="android:text">Guess?</item>
    <item name="android:fontFamily">@font/roboto</item>
    <item name="android:visibility">invisible</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@android:drawable/editbox_background</item>
    <item name="android:padding">16dp</item>
    <item name="android:textSize">24sp</item>
</style>

In code, when the user guesses something, I try to set the text property of one of the text views (the next one still showing "Guess?") to the text the user just guessed. To get to those text views, I iterate through the children of the linear layout, like this:

for (tv in binding.wofGuessesList.children) {
    tv.setBackgroundColor(Color.parseColor("#90EE90"))
    tv.text = "no such property!"
    tv.setText = "no such property!"

My question: There are dozens, if not hundreds, of properties, methods and callbacks available for those tv's, and I show here setting the background color to that green you see in the screenshot. But not the text??? What am I missing?

Thank you!

I have a bunch of text views in a linear layout. The user's interaction with them is view-only: If the user guesses correctly (using voice), that guess will show in one of the text views. It looks like this (focus on the green boxes):

The pertinent XML looks like this:

<ScrollView
    android:id="@+id/wof_guesses"
    ...
    >

    <LinearLayout
        android:id="@+id/wof_guesses_list"
        ...
        android:orientation="vertical">

        <TextView
            android:id="@+id/wof_guess0"
            style="@style/WofGuess" />

        <TextView
            android:id="@+id/wof_guess1"
            style="@style/WofGuess" />

        <TextView
            android:id="@+id/wof_guess2"
            style="@style/WofGuess" />

        <TextView
            android:id="@+id/wof_guess3"
            style="@style/WofGuess" />

        ...

    </LinearLayout>
</ScrollView>

And this is the style XML:

<style name="WofGuess" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/mentor_grey_100</item>
    <item name="android:text">Guess?</item>
    <item name="android:fontFamily">@font/roboto</item>
    <item name="android:visibility">invisible</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@android:drawable/editbox_background</item>
    <item name="android:padding">16dp</item>
    <item name="android:textSize">24sp</item>
</style>

In code, when the user guesses something, I try to set the text property of one of the text views (the next one still showing "Guess?") to the text the user just guessed. To get to those text views, I iterate through the children of the linear layout, like this:

for (tv in binding.wofGuessesList.children) {
    tv.setBackgroundColor(Color.parseColor("#90EE90"))
    tv.text = "no such property!"
    tv.setText = "no such property!"

My question: There are dozens, if not hundreds, of properties, methods and callbacks available for those tv's, and I show here setting the background color to that green you see in the screenshot. But not the text??? What am I missing?

Thank you!

Share Improve this question asked Feb 13 at 17:38 Yanay LehaviYanay Lehavi 2514 silver badges16 bronze badges 2
  • 1 You need to cast the type of View to TextView. (tv as? TextView)?.setText("no such property!"). The type of tv here is type View. – OhhhThatVarun Commented Feb 13 at 17:48
  • Thank you, worked like a charm and I wish I hadn't spent 2 days before posting the question :/ – Yanay Lehavi Commented Feb 13 at 18:07
Add a comment  | 

1 Answer 1

Reset to default 1

while you didnt provide a full example of what you are doing this code comes into suspect

for (tv in binding.wofGuessesList.children) {
    tv.setBackgroundColor(Color.parseColor("#90EE90"))
    tv.text = "no such property!"
    tv.setText = "no such property!"

tv here is likely View and not a TextView which has the .setText or .text you are looking for. You need to cast tv as a TextView.

But also why are you looping through the children and not just reference them by their id's? You are creating unnecessary overhead

本文标签: Android Linear Layout Setting text view value from codeStack Overflow