- Learning Concurrency in Kotlin
- Miguel Angel Castiblanco Torres
- 134字
- 2021-08-05 10:46:49
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.
推薦閱讀
- Data Visualization with D3 4.x Cookbook(Second Edition)
- Java語言程序設計
- 小程序實戰視頻課:微信小程序開發全案精講
- ThinkPHP 5實戰
- Android Studio Essentials
- Spring Boot+Spring Cloud+Vue+Element項目實戰:手把手教你開發權限管理系統
- 算法精粹:經典計算機科學問題的Python實現
- Scratch真好玩:教小孩學編程
- Java性能權威指南(第2版)
- Python機器學習基礎教程
- 計算機應用基礎教程(Windows 7+Office 2010)
- C#面向對象程序設計(第2版)
- 從零開始學算法:基于Python
- Perl 6 Deep Dive
- 程序員的算法趣題2