android-如何从MutableList获取属性列表

时间:2019-04-27 06:17:59

标签: android android-architecture-components android-livedata android-mvvm

这是我的JSON:

{
  "cats": [
    {
      "id": "2",
      "uid": "2",
      "name": "English",
      "date_update": "2019-04-22 15:31:00",
      "numCards": 0
    }
  ]
}

我有这两节课:

  data class CatModelStr(
    val cats: List<Cat>
)
data class Cat(
    val date_update: String,
    val id: String,
    val name: String,
    val numCards: Int,
    val uid: String
)

我正在使用MVVM和android体系结构组件。这是我用于获取数据的模型类:

class CategoryModel(private val netManager: NetManager) {
var dateChanges: String = "null";

fun getCats(): MutableLiveData<MutableList<CatModelStr>> {
    var list = MutableLiveData<MutableList<CatModelStr>>();
    if (netManager.isConnected!!) {
        list = getCatsOnline();
    }
    return list
}

private fun getCatsOnline(): MutableLiveData<MutableList<CatModelStr>> {
    var list:MutableLiveData<MutableList<CatModelStr>> =MutableLiveData()
    val getCats = ApiConnection.client.create(Category::class.java)

    getCats.getCats(MyApp().uid, dateChanges)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            { success ->
                list += success
            },{
                    error->
                Log.v("this","ErrorGetCats "+ error.localizedMessage);
            }
        )

    return list;
}

operator fun <T> MutableLiveData<MutableList<T>>.plusAssign(values: List<T>) {
    val value = this.value ?: arrayListOf()
    value.addAll(values)
    this.value = value
}

我有一个viewModel和用于获取数据的活动,它工作正常。问题是这样的,我想从MutableLiveData中获取cat值(cat中的json属性)。

这是我的活动代码:

vm.getCats().observe(this, Observer {
        if(it!=null) {
            rc_cats.visibility= View.VISIBLE
            pb.visibility=View.GONE
            catAdapter.reloadData(it)
        }
    })

值是MutableList<CatModelStr>,我需要猫。

如何使Cat脱离MutableList?

1 个答案:

答案 0 :(得分:1)

我得到的是,您不需要响应中的MutableList列表,而是您需要CatModelStr

您需要从一开始就进行以下更改。

class CategoryModel(private val netManager: NetManager) {
var dateChanges: String = "null";

fun getCats(): MutableLiveData<CatModelStr> {
    var list = MutableLiveData<CatModelStr>();
    if (netManager.isConnected!!) {
        list = getCatsOnline();
    }
    return list
}

private fun getCatsOnline(): MutableLiveData<CatModelStr> {
    var list:MutableLiveData<CatModelStr> = MutableLiveData()
    val getCats = ApiConnection.client.create(Category::class.java)

    getCats.getCats(MyApp().uid, dateChanges)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            { success ->
                list.setValue(success)
            },{
                    error->
                Log.v("this","ErrorGetCats "+ error.localizedMessage);
                list.setValue(null)
            }
        )

    return list;
}

现在,如果观察到这一点,您将得到CatModelStr而不是MutableList,原因是您的JSON以CatModelStr格式提供了数据,您正试图在MutableList也将不起作用,并且您不能在给定格式的情况下解析JSON。