質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

1回答

611閲覧

Jetpack Composeのテキストに、Listに入っている要素を表示させたいが、やり方が分からない

konn_

総合スコア28

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2022/09/11 12:14

編集2022/09/12 06:23

前提

Jetpack Composeで、OpenWeatherMapのAPIを使用した天気予報アプリを制作しています。
その天気予報アプリでは、7日分の日にち、天気のアイコン、最高気温、最低気温を取得して表示させます。
そこで、疑問点のアドバイスや、技術に関するアドバイスを頂きたいです。

天気のアイコンを正常に表示させる処理や、日時がUNIXになっているのでその処理をしないといけないのですが、それはまだできていません。
また、非同期処理をしないとエラーが出るようなのでそれもこれからしようと思っています。

疑問点

・WeatherGetjsonクラスのweatherdataというリストに、7日分の情報(日時、天気など...)が全て入れているのですが、今のコードのままで要素を指定して取り出すことができるのか。(今のコードだと、TextにgetJson()が入っていて、リストのすべての情報を一括して表示させることしかできない。)

・WeatherScreenクラスの各テキストに、上記weatherdataリストの各要素を表示させたいのですが、WeatherScreen クラスに、weatherdataリストをもっていくのか、getJsonをもっていくのか、そしてどのように持っていくことができるのかが分からないです。(そもそも持っていくという表現が正しくないと思うのですが...)

該当のソースコード

WeatherScreen.ktと、WeatherGetjson.ktの2つのファイルを使用しています。
WeatherScreeクラスには、Jetpack Composeで、天気を表示させる画面のUIを完成させました。

WeatherScreen

1package com.example.weatherapp 2 3import androidx.compose.foundation.Image 4import androidx.compose.foundation.background 5import androidx.compose.foundation.layout.* 6import androidx.compose.foundation.rememberScrollState 7import androidx.compose.foundation.shape.RoundedCornerShape 8import androidx.compose.foundation.verticalScroll 9import androidx.compose.material.MaterialTheme 10import androidx.compose.material.Text 11import androidx.compose.runtime.Composable 12import androidx.compose.ui.Modifier 13 14import androidx.compose.ui.res.painterResource 15import androidx.compose.ui.text.font.FontWeight 16import androidx.compose.ui.tooling.preview.Preview 17import androidx.compose.ui.unit.dp 18import androidx.compose.ui.unit.sp 19import com.example.weatherapp.ui.theme.WeatherAppTheme 20 21 22@Composable 23fun WeatherDisplay() { 24 25 Column( 26 modifier = Modifier 27 .padding(all = 8.dp) 28 .background(color = MaterialTheme.colors.background) 29 ) { 30 31 Box( 32 modifier = Modifier 33 .background( 34 color = MaterialTheme.colors.primaryVariant, 35 shape = RoundedCornerShape(15) 36 ) 37 .padding(start = 20.dp, top = 10.dp, end = 20.dp, bottom = 20.dp) 38 .fillMaxWidth() 39 .requiredHeight(150.dp) 40 41 ) { 42 43 Row { 44 Column { 45 Text( 46 text = "日にち", 47 color = MaterialTheme.colors.surface, 48 fontSize = 30.sp 49 ) 50 51 Spacer(modifier = Modifier.height(4.dp)) 52 53 Image( 54 painter = painterResource(R.drawable.hitu), 55 contentDescription = "weather pictures ", 56 modifier = Modifier.size(120.dp) 57 ) 58 } 59 60 Spacer(modifier = Modifier.width(50.dp)) 61 62 63 Column( 64 65 modifier = Modifier.fillMaxSize(), 66 verticalArrangement = Arrangement.Center 67 68 ) { 69 Text( 70 text = "20/11", 71 fontSize = 50.sp, 72 color = MaterialTheme.colors.surface 73 ) 74 75 Text( 76 text = "50%", 77 fontSize = 50.sp, 78 color = MaterialTheme.colors.surface 79 ) 80 81 } 82 } 83 } 84 85 Spacer(modifier = Modifier.height(30.dp)) 86 87 Column( 88 modifier = Modifier 89 .verticalScroll(rememberScrollState()) 90 ) { 91 92 Row( 93 Modifier.padding(bottom = 30.dp) 94 ) { 95 Text( 96 text = getJson(), 97 Modifier.weight(1f), 98 fontSize = 20.sp, 99 fontWeight = FontWeight.Bold 100 ) 101 Image( 102 painter = painterResource(R.drawable.hitu), 103 contentDescription = "weather pictures ", 104 Modifier.weight(1f) 105 ) 106 Text(text = "18/11", Modifier.weight(1f), fontSize = 20.sp) 107 Text(text = "30%", Modifier.weight(1f), fontSize = 20.sp) 108 } 109 110 } 111 } 112} 113 114 115@Preview 116@Composable 117fun weatherDisplayPreview() { 118 WeatherAppTheme { 119 WeatherDisplay() 120 } 121} 122 123

WeatherGetjsonクラスでは、Jsonを取得して、listの中に入れることができました。

WeatherGetjson

1package com.example.weatherapp 2 3import kotlinx.coroutines.Dispatchers 4import kotlinx.coroutines.GlobalScope 5import kotlinx.coroutines.launch 6import kotlinx.coroutines.withContext 7import org.json.JSONException 8import org.json.JSONObject 9import java.io.BufferedReader 10import java.io.IOException 11import java.io.InputStreamReader 12 13import java.net.URL 14 15class WeatherGetjson() { 16 17} 18 19 20suspend fun weatherTask() { 21 22 val apiKey = " //APIKey" 23 val apiUrl = 24 "https://api.openweathermap.org/data/2.5/onecall?lat=35.68&lon=139.70&exclude=current,minutely,hourly,alerts&appid=${apiKey}" 25 26 GlobalScope.launch { 27 val result = weatherBackgroundTast(apiUrl) 28 weatherJsonTask(result) 29 } 30 31} 32 33 private suspend fun weatherBackgroundTast(apiUrl: String): String { 34 35 val response = withContext(Dispatchers.IO) { 36 37 var weatherdata = "" 38 39 40 41 try { 42 val url = URL(apiUrl) 43 val br = BufferedReader(InputStreamReader(url.openStream())) 44 val str = br.readText() 45 46 } catch (e: IOException) { 47 e.printStackTrace() 48 } catch (e: JSONException) { 49 e.printStackTrace() 50 } 51 return@withContext weatherdata 52 53 } 54 return response 55} 56 57 58private fun weatherJsonTask(result: String) { 59 60 //取得した情報をしまう 61 var list: ArrayList<String> = arrayListOf() 62 63 val json = JSONObject(result) 64 val weatherAry = json.getJSONArray("daily") 65 66 67 for (i in 0..7) { 68 val weatherObj = weatherAry.getJSONObject(i) 69 var temp = weatherObj.getJSONObject("temp") 70 var weather = weatherObj.getJSONArray("weather") 71 var weatherToObj = weather.getJSONObject(0) 72 73 var date = weatherObj.get("dt") 74 var icon = weatherToObj.get("icon") 75 var pop = weatherObj.get("pop") 76 var min = temp.get("min") 77 var max = temp.get("max") 78 79 list += (arrayListOf(date, icon, pop, min, max).toString()) 80 } 81 82 83 84} 85 86 87 88

試したこと

・Googleにて解決法を探った。
・それでも解決しなかったので、WeatherScreenのテキストにいろいろな文字を当てはめた

もしよろしければアドバイスよろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自作クラスとかが見当たらず、順番に実行するコードが並んでいるだけです。
オブジェクト指向とは何なのか辺りが分かっていない感じがします。

投稿2022/09/12 03:00

jimbe

総合スコア12646

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

konn_

2022/09/12 06:26

確かにそうですね。 WeatherGetjsonクラスを編集してみました。 あとはどの辺りを治すべきでしょうか。 もしよろしければアドバイスお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問