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

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

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

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

Kotlin

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

Q&A

0回答

985閲覧

ListViewをカスタマイズするために作ったレイアウトファイルに、Spinnerがある。違うボタンタップでSpinnerの項目内容を変更して表示したい

qishihuang

総合スコア2

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2020/11/02 14:31

発生している問題・エラーメッセージ

現段階のコードではアプリが起動しない

該当のソースコード

kotlin

1class MainActivity : AppCompatActivity() { 2 3 lateinit var mCustomAdapter: CustomAdapter 4 lateinit var mAnimalList: ArrayList<Animal> 5 private val spinnerItems1 = arrayOf("L","mL","μL") 6 private val spinnerItems2 = arrayOf("g","mg","μg") 7 8 override fun onCreate(savedInstanceState: Bundle?) { 9 super.onCreate(savedInstanceState) 10 setContentView(R.layout.activity_main) 11 12 //この4行を記述するとアプリが開かなくなる 13 val spinner = findViewById<Spinner>(R.id.spinner) 14 val adapter = ArrayAdapter.createFromResource(this, R.array.a, android.R.layout.simple_spinner_item); 15 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) 16 spinner.adapter = adapter 17 18 19 mAnimalList = arrayListOf() 20 val listView = findViewById<ListView>(R.id.listView) 21 // CustomAdapterの生成と設定 22 mCustomAdapter = CustomAdapter(this, mAnimalList) 23 listView.adapter = mCustomAdapter 24 //ボタンの処理追加 25 val btClick = findViewById<Button>(R.id.one) 26 val btClick2 = findViewById<Button>(R.id.two) 27 val Listener = HelloListener() 28 btClick.setOnClickListener(Listener) 29 btClick2.setOnClickListener(Listener) 30 } 31 32 private inner class HelloListener() : View.OnClickListener{ 33 override fun onClick(v: View?) { 34 val dog = Animal("いぬ", 3) 35 mAnimalList.add(dog) 36 mCustomAdapter.notifyDataSetChanged() 37 } 38 } 39}

kotlin

1class CustomAdapter(context: Context, var mAnimalList: List<Animal>) : ArrayAdapter<Animal>(context, 0, mAnimalList) { 2 3 private val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater 4 5 override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 6 // Animalの取得 7 val animal = mAnimalList[position] 8 9 // レイアウトの設定 10 var view = convertView 11 if (convertView == null) { 12 view = layoutInflater.inflate(R.layout.row, parent, false) 13 } 14 15 // 各Viewの設定 16 val name = view?.findViewById<TextView>(R.id.name) 17 name?.text = animal.name 18 19 val age = view?.findViewById<TextView>(R.id.age) 20 age?.text = "${animal.age} 才" 21 22 return view!! 23 } 24}

kotlin

1data class Animal ( 2 val name: String, 3 val age: Int 4)

kotlin

1<resources> 2 <string name="app_name">H</string> 3 <string name="nyuuryoku">入力してください</string> 4 <string name="tuika">追加</string> 5 <string name="datanai">データは存在しません</string> 6 7 <string-array name="a"> 8 <item>a</item> 9 <item>a</item> 10 <item>a</item> 11 </string-array> 12 <string-array name="b"> 13 <item>b</item> 14 <item>b</item> 15 <item>b</item> 16 </string-array> 17</resources>

kotlin

1row.xml 2<?xml version="1.0" encoding="utf-8"?> 3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="horizontal" android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <TextView 8 android:id="@+id/name" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content"/> 11 12 <TextView 13 android:id="@+id/age" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content"/> 16 17 <Spinner 18 android:id="@+id/spinner" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" /> 21</LinearLayout>

kotlin

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <Button 9 android:id="@+id/one" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="1"/> 13 14 <Button 15 android:id="@+id/two" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="2"/> 19 20 <ListView 21 android:id="@+id/listView" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content"/> 24</LinearLayout>

補足情報(FW/ツールのバージョンなど)

android studio 3.5.3

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問