1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android studio(Java)でspinnerで文字配列を表示するソース

Last updated at Posted at 2019-05-10

自分用のメモです。
タイトル通りですが、文字配列を定義しておいて、spinner(ドロップダウンメニュー)で、表示させ、それをテキスト(TextView)に表示するソースです。

スクリーンショット 2019-05-10 13.57.07.png
MainActivity.java
package com.example.ktakeda.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    TextView resultTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        initViews();
        
    }

    private void findViews() {
        // objectを取得
        spinner = (Spinner)findViewById(R.id.spinner);
        resultTextView = (TextView)findViewById(R.id.result);
    }

    private void initViews() {
        // spinner listner
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                // 選択されているアイテムのIndexを取得
                int idx = (int)spinner.getSelectedItemPosition();
                Log.d("idx", "idx:" + idx);

                // String resultText = String.valueOf(idx + 1) + ":" + (String)spinner.getSelectedItem(); <-修正しました↓
                String resultText = String.valueOf((idx + 1) + ":" + (String)spinner.getSelectedItem());

                // 選択されているアイテムを取得
                resultTextView.setText(resultText);
                Log.d("selected", "result" + (String)spinner.getSelectedItem());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 選択されたなかった時の処理
            }
        });
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="370dp"
        android:layout_height="20dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:entries="@array/work"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:text="TextView"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spinner" />

</android.support.constraint.ConstraintLayout>
strings.xml
<resources>
    <string name="app_name">spinner</string>
    <string-array name="work">
        <item>大工</item>
        <item>配管工</item>
        <item>土木</item>
        <item>建築士</item>
        <item>電気工事</item>
    </string-array>
</resources>

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?