0

This is my JSON :

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

I've these two classes:

  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
)

I'm using MVVM and android architecture components. This is my model class for getting the data:

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
}

I have a viewModel and activity for getting the data and it works fine. The problem is this, I want to get cat values (the json properties inside cat) out of my MutableLiveData.

This is my activity code:

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

The value is MutableList<CatModelStr> and I need Cat.

How can I get Cat out of MutableList?

Dale K
  • 25,246
  • 15
  • 42
  • 71
Navid Abutorab
  • 1,667
  • 7
  • 31
  • 58

1 Answers1

1

What I am getting is that you don't need MutableList list from your response instead you need CatModelStr

you need to make the following changes from starting i guess.

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;
}

and now if observe this you will get CatModelStr instead of MutableList and the reason is that your JSON is giving you data in format CatModelStr, you are trying to get this unnecessarily in MutableList which also won't work and you can't parse your JSON given the format.

vikas kumar
  • 10,447
  • 2
  • 46
  • 52