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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

String

Stringは、ゼロ以上の文字から連続してできた文字の集合を扱うデータ型です。基本的にテキストを表すために使われます。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

0回答

930閲覧

tyspinnerから選択した文字列を取得できない。

apeirogon0813

総合スコア117

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

String

Stringは、ゼロ以上の文字から連続してできた文字の集合を扱うデータ型です。基本的にテキストを表すために使われます。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2020/05/29 22:05

編集2020/05/29 22:15
switch構文のところを switch(parent.getId()) としたところ、文字列を受け取ることができました。 しかし、なぜviewではなくparentから取得できるのか理解できません。 ご教示願います。 ################################ 以下のように、spinnerを5つ配置し、 public void onItemSelectedにて、選択肢入力後の文字列を受け取ろうとしたところ、 うまく受け取れません。 Log.dにてデバックを行うと、onItemSelectedの Log.d("PlaceSample", "onItemselected"); は呼び出されましたが、 switch構文の中の Log.d("PlaceSample", "sound1" +item+ "a"); は呼び出されておりませんでした。 試しに、 String item = (String) spinner.getSelectedItem();を String item = (String) spinner.getSelectedItem().**toString()**; にしてみましたが、変わりませんでした。 switch判定に用いているview.getId()が問題なのでしょうか。 ご教示願います。 ```JAVA package com.example.runwithmusic; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.Spinner; import androidx.appcompat.app.AppCompatActivity; public class SubActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener { private String sound1; private String sound2; private String sound3; private String threshold1; private String threshold2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("PlaceSample", "plof intent"); setContentView(R.layout.activity_sub); Spinner spinner1 = findViewById(R.id.spinner1); Spinner spinner2 = findViewById(R.id.spinner2); Spinner spinner3 = findViewById(R.id.spinner3); Spinner spinner4 = findViewById(R.id.spinner4); Spinner spinner5 = findViewById(R.id.spinner5); spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this); spinner2.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this); spinner3.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this); spinner4.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this); spinner5.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this); Button backButton = findViewById(R.id.backButton); backButton.setOnClickListener((View.OnClickListener) this); } @Override public void onItemSelected (AdapterView<?> parent, View view, int position, long id) { Log.d("PlaceSample", "onItemselected"); Spinner spinner = (Spinner)parent; String item = (String) spinner.getSelectedItem(); switch (view.getId()) { case R.id.spinner1: sound1 = item; Log.d("PlaceSample", "sound1" +item+ "a"); break; case R.id.spinner2: threshold1 = item; break; case R.id.spinner3: sound2 = item; break; case R.id.spinner4: threshold2 = item; break; case R.id.spinner5: sound3 = item; break; } } @Override public void onNothingSelected(AdapterView<?> parent) { } @Override public void onClick(View view) { if (view.getId() == R.id.backButton) { Intent intent = new Intent(); intent.putExtra("SOUND1", sound1); intent.putExtra("SOUND2", sound2); intent.putExtra("SOUND3", sound3); intent.putExtra("THRESHOLD1", threshold1); intent.putExtra("THRESHOLD2", threshold2); //Resultを入れる setResult(RESULT_OK, intent); //intentの終了 finish(); } } } ``` ```activity_sub.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#7000BCD4" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="250dp" android:gravity="right" android:orientation="vertical" app:layout_constraintTop_toTopOf="parent" tools:layout_editor_absoluteX="1dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_horizontal" android:text="0km/s~閾値1の曲" /> <Spinner android:id="@+id/spinner1" android:layout_width="200dp" android:layout_height="match_parent" android:entries="@array/list" android:spinnerMode="dialog"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_horizontal" android:text="閾値1" /> <Spinner android:id="@+id/spinner2" android:layout_width="200dp" android:layout_height="match_parent" android:entries="@array/num" android:spinnerMode="dialog"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_horizontal" android:text="閾値1~閾値2の曲" /> <Spinner android:id="@+id/spinner3" android:layout_width="200dp" android:layout_height="match_parent" android:entries="@array/list" android:spinnerMode="dialog"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_horizontal" android:text="閾値2" /> <Spinner android:id="@+id/spinner4" android:layout_width="200dp" android:layout_height="match_parent" android:entries="@array/num" android:spinnerMode="dialog"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_horizontal" android:text="閾値2~∞の曲" /> <Spinner android:id="@+id/spinner5" android:layout_width="200dp" android:layout_height="match_parent" android:entries="@array/list" android:spinnerMode="dialog"/> </LinearLayout> <Button android:id="@+id/backButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="変更" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout> ``` ```string.xml <resources> <string name="app_name">Run with Music</string> <string-array name="list"> <item>Bach.mp3</item> <item>Dies_irae.mp3</item> <item>Dvorak.mp3</item> <item>OffenBach.mp3</item> </string-array> <string-array name="num"> <item>1.0</item> <item>2.0</item> <item>3.0</item> <item>4.0</item> <item>5.0</item> <item>6.0</item> <item>7.0</item> <item>8.0</item> <item>9.0</item> <item>10.0</item> <item>11.0</item> <item>12.0</item> <item>13.0</item> <item>14.0</item> <item>15.0</item> <item>16.0</item> <item>17.0</item> <item>18.0</item> <item>19.0</item> <item>20.0</item> <item>21.0</item> <item>22.0</item> <item>23.0</item> <item>24.0</item> <item>25.0</item> <item>26v</item> <item>27v</item> <item>28.0</item> <item>29.0</item> <item>30.0</item> </string-array> </resources> コード ```

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問