官术网_书友最值得收藏!

Displaying the amount of news that were processed

Let's put a TextView in our layout and display the amount of news that were processed from the feed. Notice that the TextView will be located below the progress bar because of the property app:layout_constraintBottom_toBottomOf:

<android.support.constraint.ConstraintLayout ...>
<ProgressBar ...>
<TextView
android:id="@+id/newsCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/progressBar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

In order to display the amount of news, we will obtain the TextView by its identifier and set the text to be the amount of news that were obtained:

launch(dispatcher) {
val headlines = fetchRssHeadlines()
val newsCount = findViewById<TextView>(R.id.newsCount)
newsCount.text = "Found ${headlines.size} News"
}

If executed, this code will crash the application with a CalledFromWrongThreadException, as explained earlier in the chapter. This makes sense because all the content of our coroutine is being executed in a background thread, and UI updates must happen on the UI thread.

主站蜘蛛池模板: 元谋县| 泰兴市| 项城市| 承德市| 高密市| 安乡县| 波密县| 上思县| 新巴尔虎左旗| 城口县| 南汇区| 鸡东县| 固安县| 郁南县| 安泽县| 淄博市| 桃园县| 陆良县| 南宫市| 巴林右旗| 牟定县| 陇西县| 即墨市| 鹤庆县| 凤翔县| 靖远县| 普陀区| 桐城市| 虞城县| 秦安县| 海宁市| 宣威市| 习水县| 绿春县| 台江县| 竹溪县| 荥经县| 吐鲁番市| 侯马市| 灵台县| 思南县|