iT邦幫忙

2023 iThome 鐵人賽

DAY 28
0
自我挑戰組

Practice again& again.系列 第 28

Android - Http Request 前置作業

  • 分享至 

  • xImage
  •  

使用 Open API - REST Countries 作為 HTTP Request 範例

製作資料呈現 UI

使用 Compose UI 的 LazyVerticalGridLazyColumn 呈現資料 List,取代 RecyclerView

@Composable
@Preview(showBackground = true)
fun CountryList(list: List<CountryData>){
    LazyVerticalGrid(
        columns = GridCells.Fixed(1),
        contentPadding = PaddingValues(
            start = 12.dp,
            top = 16.dp,
            end = 12.dp,
            bottom = 16.dp
        ),
        content = {
            items(list.size) { index ->
                Card(
                    modifier = Modifier
                        .padding(4.dp)
                        .fillMaxWidth()
                ) {
                    Text(
                        text = list[index].name,
                        fontWeight = FontWeight.Bold,
                        fontSize = 30.sp,
                        color = Color(0xFFFFFFFF),
                        textAlign = TextAlign.Center,
                        modifier = Modifier.padding(16.dp)
                    )
                }
            }
        }
    )
}

宣告所需欄位於 Data class

依據 Response 欄位,改寫 Data class 內容:

@JsonClass(generateAdapter = true)
data class CountryData(
    val name: NameData,
    val tld: List<String>,
    val cca2: String,
    val ccn3: String,
    val cca3: String,
    val independent: Boolean,
    val status: String,
    val unMember: Boolean,
    val currencies: Map<String, CurrencyData>,
    val idd: IddData,
    val capital: List<String>,
    val altSpellings: List<String>,
    val region: String,
    val subregion: String,
    val languages: Map<String, String>
)

data class NameData(
    val common: String,
    val official: String,
    val nativeName: Map<String, NativeNameData>
)

data class NativeNameData(
    val official: String,
    val common: String
)

data class CurrencyData(
    val name: String,
    val symbol: String
)

data class IddData(
    val root: String,
    val suffixes: List<String>
)


上一篇
Android - 建立 Data class
下一篇
Android - Retrofit
系列文
Practice again& again.30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言