Android tv
上一篇我們使用了browsefragment,再來就把資料帶到裡面
首先先把Json檔拉近row的資料夾
接著新增對應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)
}