休息活動
package com.example.internet_ex
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.*
import kotlinx.coroutines.launch
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
import java.lang.StringBuilder
data class Owner(val login: String)
data class Repo(val name: String, val owner: Owner, val url: String)
data class Contributor(val login: String, val contributions: Int)
interface RestApi {
@GET("users/{user}/repos")
suspend fun listRepos(@Path("user") user: String): List<Repo>
@GET("/repos/{owner}/{repo}/contributors")
suspend fun contributors(
@Path("owner") owner: String,
@Path("repo") repo: String
): List<Contributor>
}
class MyViewModel : ViewModel() {
private val baseURL = "https://api.github.com/"
private lateinit var api: RestApi
val userName = MutableLiveData<String>()
val response = MutableLiveData<String>()
init {
retrofitInit()
refreshData()
}
fun refreshData() {
viewModelScope.launch {
try {
val repos = api.listRepos(userName.toString())
response.value = StringBuilder().apply {
repos.forEach {
append(it.name)
append(" - ")
append(it.owner.login)
append("\n")
}
}.toString()
} catch (e: Exception) {
response.value = "Failed to connect to the server"
}
}
}
private fun retrofitInit() {
val retrofit = Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(MoshiConverterFactory.create())
.build()
api = retrofit.create(RestApi::class.java)
}
}
class RestActivity : AppCompatActivity() {
private lateinit var myViewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_rest)
myViewModel = ViewModelProvider(this).get(MyViewModel::class.java)
// problem
myViewModel.userName.observe(this) {
findViewById<Button>(R.id.queryBtn).setOnClickListener {
myViewModel.userName.value = findViewById<Button>(R.id.nameText).toString()
myViewModel.refreshData()
}
}
myViewModel.response.observe(this) {
findViewById<TextView>(R.id.textResponse).text = it
findViewById<Button>(R.id.queryBtn).setOnClickListener {
}
}
}
}
活動休息
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RestActivity">
<TextView
android:id="@ id/textResponse"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/queryBtn" />
<Button
android:id="@ id/queryBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Query"
app:layout_constraintStart_toEndOf="@ id/nameText"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@ id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
**activity_rest.xml**
我做了一個地方,我評論說這是一個問題,因為我想userName在ViewModel按下時更改queryBtn,但是沒有用。我想LiveData (userName)使用setOnClickLisenter命令更改ViewModel 中的,但我無法弄清楚。如果您能告訴我要額外學習什么,我將不勝感激。
uj5u.com熱心網友回復:
您的OnClickListener需要設定在observer. 至少從我可以告訴您的情況來看,不需要,observe userName因為queryBtn用于激活 a response,而不是userName已更改的事實。
但是您確實希望observe在response中更改ViewModel,并且需要在 中顯示textResponse。
下面的代碼沒有檢查正確性,我只是復制了你的代碼并洗掉了不必要的位。
findViewById<Button>(R.id.queryBtn).setOnClickListener {
myViewModel.userName.value = findViewById<EditText>(R.id.nameText).getText().toString()
myViewModel.refreshData()
}
myViewModel.response.observe(this) {
findViewById<TextView>(R.id.textResponse).text = it
}
uj5u.com熱心網友回復:
將此外部觀察者塊移動
findViewById<Button>(R.id.queryBtn).setOnClickListener {
myViewModel.userName.value = findViewById<EditText>(R.id.nameText).getText().toString()
}
在觀察者塊內部,當用戶字串更新時,呼叫重繪 資料
myViewModel.userName.observe(this) {
myViewModel.refreshData()
}
onClickListener從response觀察者塊中移除。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312835.html
上一篇:在NN訓練期間最大化一個損失并最小化另一個損失的正確方法是什么?
下一篇:將流或序列轉換為摘要流或序列
