Android ViewModel使用模板

1,创建ViewModel类

//MainViewModel.kt
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.myapplication.entity.Banner
import com.example.myapplication.network.BaseResponse
import com.example.myapplication.network.NetworkUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MainViewModel : ViewModel() {
    private val _errorLiveData: MutableLiveData<String> = MutableLiveData()
    val errorLiveData: LiveData<String> = _errorLiveData

    private val _bannerLiveData = MutableLiveData<List<Banner>>()
    val bannerLiveData:LiveData<List<Banner>> = _bannerLiveData

    fun getBanners(){
        NetworkUtil.wanAndroidApi.getBanners()
            .enqueue(object : Callback<BaseResponse<List<Banner>>> {
                override fun onResponse(
                    call: Call<BaseResponse<List<Banner>>>,
                    response: Response<BaseResponse<List<Banner>>>
                ) {
                    _bannerLiveData.value=response.body()?.data
                }

                override fun onFailure(
                    call: Call<BaseResponse<List<Banner>>>,
                    t: Throwable
                ) {
                    _errorLiveData.value = t.message.toString()
                }
            })
    }
}

2,引用ViewModel

//MainActivity.kt
class MainActivity : ComponentActivity() {
    private lateinit var mViewModel:MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
              Column(Modifier.fillMaxSize(), 
                  horizontalAlignment = Alignment.CenterHorizontally) { 
                var textState by rememberSaveable { mutableStateOf("Initial Text")}                
                mViewModel.bannerLiveData.observe(this@MainActivity){
                        textState = it.toString() }

                 Button(onClick = { mViewModel.getBanners() }) { 
                        Text(text = "获取网络信息") } 

                        Text(text = textState) 
            }
        }

        mViewModel = ViewModelProvider(this)[MainViewModel::class.java]
    }
   
}

相关推荐

  1. 【c++】模板使用

    2024-04-27 00:02:04       47 阅读
  2. C# 类模板使用

    2024-04-27 00:02:04       40 阅读
  3. Android retrofit使用模板

    2024-04-27 00:02:04       148 阅读
  4. Android ViewModel使用模板

    2024-04-27 00:02:04       35 阅读
  5. Android Room使用模板

    2024-04-27 00:02:04       35 阅读
  6. 如何正确使用 HTML 模板

    2024-04-27 00:02:04       66 阅读
  7. IDEA快捷使用-快捷键&模板

    2024-04-27 00:02:04       59 阅读
  8. C++中模板使用

    2024-04-27 00:02:04       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-27 00:02:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-27 00:02:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-27 00:02:04       82 阅读
  4. Python语言-面向对象

    2024-04-27 00:02:04       91 阅读

热门阅读

  1. 【小浩算法cpp题解】合并两个有序链表(21)

    2024-04-27 00:02:04       33 阅读
  2. nvm 错误

    2024-04-27 00:02:04       29 阅读
  3. 展开说说:Android动画使用

    2024-04-27 00:02:04       26 阅读
  4. 什么是裸金属服务器?

    2024-04-27 00:02:04       35 阅读
  5. 原型模式(上机考试抽题)

    2024-04-27 00:02:04       31 阅读
  6. 01_numpy

    2024-04-27 00:02:04       36 阅读
  7. 使用 LLaMA Factory 微调 Llama-3 中文对话模型

    2024-04-27 00:02:04       34 阅读
  8. class092 贪心经典题目专题4【左程云算法】

    2024-04-27 00:02:04       36 阅读
  9. Oracle基础1

    2024-04-27 00:02:04       25 阅读
  10. tcp通信协议

    2024-04-27 00:02:04       28 阅读
  11. 日志处理初探-日志存储

    2024-04-27 00:02:04       28 阅读
  12. 【SAP ME 12】SAP NWDS(eclipse)下载、安装,配置

    2024-04-27 00:02:04       22 阅读
  13. SQL查询一页数据过多太慢

    2024-04-27 00:02:04       37 阅读
  14. 【C++】33 搜索旋转排序数组

    2024-04-27 00:02:04       32 阅读