iT邦幫忙

2021 iThome 鐵人賽

DAY 26
0
自我挑戰組

Android kotlin &MVVM系列 第 26

Android 學習筆記26

Android tv
上一篇我們使用了browsefragment,再來就把資料帶到裡面
首先先把Json檔拉近row的資料夾
https://ithelp.ithome.com.tw/upload/images/20211005/20141791gED2bFB6Bd.png
接著新增對應Json檔的dataclass

data class MovieList(
    var data: List<Data>? = null
)

data class Data(
    var category_name: String? = null,
    var sub_categories: List<SubCategory>? = null
)

data class SubCategory(
    var sub_category_name: String? = null,
    var items: List<Item>? = null
)

data class Item(
    var species: String? = null,
    var name: String? = null,
    var imageUrl: String? = null,
    var whichRow:Int ?=null
)
data class BannerData(
    var category_name: String? = null,
    var banner_list: List<String>? = null
)

接著在mainfragment中取得資料

fun getBannerData(){
    val jsonFileString: String = resources.openRawResource(R.raw.banner).bufferedReader().use { it.readText() }
    mBannerData = Gson().fromJson(jsonFileString, BannerData::class.java)
}

fun getMovieList(){
    val jsonFileString: String = resources.openRawResource(R.raw.movielist).bufferedReader().use { it.readText() }
    mMovieList = Gson().fromJson(jsonFileString, MovieList::class.java)
}

然後在一開始取的

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    init()
    getBannerData()
    getMovieList()
    super.onViewCreated(view, savedInstanceState)
}

上一篇
Android學習筆記25
下一篇
Android 學習筆記27
系列文
Android kotlin &MVVM30

尚未有邦友留言

立即登入留言