質問編集履歴
1
ソースコードの貼り付けが漏れていたため貼り付けいたしました。申し訳ありません。
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -8,14 +8,184 @@ 
     | 
|
| 
       8 
8 
     | 
    
         | 
| 
       9 
9 
     | 
    
         
             
            ### 該当のソースコード
         
     | 
| 
       10 
10 
     | 
    
         | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
      
 11 
     | 
    
         
            +
            言語名:Kotlin
         
     | 
| 
       12 
12 
     | 
    
         
             
            ソースコード
         
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
      
 13 
     | 
    
         
            +
            【MainActivity.kt】
         
     | 
| 
      
 14 
     | 
    
         
            +
            package com.example.kenta.intentsample
         
     | 
| 
       14 
15 
     | 
    
         | 
| 
      
 16 
     | 
    
         
            +
            import android.content.Intent
         
     | 
| 
      
 17 
     | 
    
         
            +
            import androidx.appcompat.app.AppCompatActivity
         
     | 
| 
      
 18 
     | 
    
         
            +
            import android.os.Bundle
         
     | 
| 
      
 19 
     | 
    
         
            +
            import android.view.View
         
     | 
| 
      
 20 
     | 
    
         
            +
            import android.widget.AdapterView
         
     | 
| 
      
 21 
     | 
    
         
            +
            import android.widget.ListView
         
     | 
| 
      
 22 
     | 
    
         
            +
            import android.widget.SimpleAdapter
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
            class MainActivity : AppCompatActivity() {
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                override fun onCreate(savedInstanceState: Bundle?) {
         
     | 
| 
      
 27 
     | 
    
         
            +
                    super.onCreate(savedInstanceState)
         
     | 
| 
      
 28 
     | 
    
         
            +
                    setContentView(R.layout.activity_main)
         
     | 
| 
      
 29 
     | 
    
         
            +
                    //画面部品ListViewを取得
         
     | 
| 
      
 30 
     | 
    
         
            +
                    val lvMenu = findViewById<ListView>(R.id.lvMenu)
         
     | 
| 
      
 31 
     | 
    
         
            +
                    //SimpleAdapterで使用するMutableListオブジェクトを用意。
         
     | 
| 
      
 32 
     | 
    
         
            +
                    val menuList: MutableList<MutableMap<String, String>> = mutableListOf()
         
     | 
| 
      
 33 
     | 
    
         
            +
                    //「から揚げ定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 34 
     | 
    
         
            +
                    var menu = mutableMapOf("name" to "から揚げ定食", "price" to "800円")
         
     | 
| 
      
 35 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 36 
     | 
    
         
            +
                    //「ハンバーグ定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 37 
     | 
    
         
            +
                    menu = mutableMapOf("name" to "ハンバーグ定食", "price" to "850円")
         
     | 
| 
      
 38 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    //「生姜焼き定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 40 
     | 
    
         
            +
                    menu = mutableMapOf("name" to "生姜焼き定食", "price" to "850円")
         
     | 
| 
      
 41 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 42 
     | 
    
         
            +
                    //「ステーキ定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 43 
     | 
    
         
            +
                    menu = mutableMapOf("name" to "ステーキ定食", "price" to "1000円")
         
     | 
| 
      
 44 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 45 
     | 
    
         
            +
                    //「野菜炒め定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 46 
     | 
    
         
            +
                    menu = mutableMapOf("name" to "野菜炒め定食", "price" to "750円")
         
     | 
| 
      
 47 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 48 
     | 
    
         
            +
                    //「とんかつ定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 49 
     | 
    
         
            +
                    menu = mutableMapOf("name" to "とんかつ定食", "price" to "900円")
         
     | 
| 
      
 50 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 51 
     | 
    
         
            +
                    //「ミンチかつ定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 52 
     | 
    
         
            +
                    menu = mutableMapOf("name" to "ミンチかつ定食", "price" to "850円")
         
     | 
| 
      
 53 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 54 
     | 
    
         
            +
                    //「チキンカツ定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 55 
     | 
    
         
            +
                    menu = mutableMapOf("name" to "チキンカツ定食", "price" to "900円")
         
     | 
| 
      
 56 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 57 
     | 
    
         
            +
                    //「コロッケ定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 58 
     | 
    
         
            +
                    menu = mutableMapOf("name" to "コロッケ定食", "price" to "850円")
         
     | 
| 
      
 59 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 60 
     | 
    
         
            +
                    //「焼き魚定食」のデータを格納するMapオブジェクトの用意とmenuListへのデータ登録。
         
     | 
| 
      
 61 
     | 
    
         
            +
                    menu = mutableMapOf("name" to "焼き魚定食", "price" to "750円")
         
     | 
| 
      
 62 
     | 
    
         
            +
                    menuList.add(menu)
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
                    //SimpleAdapter第4引数from用データの用意。
         
     | 
| 
      
 65 
     | 
    
         
            +
                    val from = arrayOf("name", "price")
         
     | 
| 
      
 66 
     | 
    
         
            +
                    //SimpleAdapter第5引数to用データの用意。
         
     | 
| 
      
 67 
     | 
    
         
            +
                    val to = intArrayOf(android.R.id.text1, android.R.id.text2)
         
     | 
| 
      
 68 
     | 
    
         
            +
                    //SimpleAdapterを生成。
         
     | 
| 
      
 69 
     | 
    
         
            +
                    val adapter = SimpleAdapter(applicationContext, menuList, android.R.layout.simple_list_item_2, from, to)
         
     | 
| 
      
 70 
     | 
    
         
            +
                    //アダプタの登録。
         
     | 
| 
      
 71 
     | 
    
         
            +
                    lvMenu.adapter = adapter
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
                    //リストタップのリスナクラス登録
         
     | 
| 
      
 74 
     | 
    
         
            +
                    lvMenu.onItemClickListener = ListItemClickListener()
         
     | 
| 
      
 75 
     | 
    
         
            +
                }
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
                //リストがタップされた時の処理が記述されたメンバクラス。
         
     | 
| 
      
 78 
     | 
    
         
            +
                private inner class ListItemClickListener : AdapterView.OnItemClickListener {
         
     | 
| 
      
 79 
     | 
    
         
            +
                    override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
         
     | 
| 
      
 80 
     | 
    
         
            +
                        //タップされた行のデータを取得。SimpleAdapterでは1行分のデータはMutableMap型!
         
     | 
| 
      
 81 
     | 
    
         
            +
                        val item = parent.getItemIdAtPosition(position) as MutableMap<String, String>
         
     | 
| 
      
 82 
     | 
    
         
            +
                        //定食名と金額を取得。
         
     | 
| 
      
 83 
     | 
    
         
            +
                        val menuName = item["name"]
         
     | 
| 
      
 84 
     | 
    
         
            +
                        val menuPrice = item["price"]
         
     | 
| 
      
 85 
     | 
    
         
            +
                        //インテントオブジェクトを生成。
         
     | 
| 
      
 86 
     | 
    
         
            +
                        val intent = Intent(applicationContext, MenuThanksActivity::class.java)
         
     | 
| 
      
 87 
     | 
    
         
            +
                        //第2画面に送るデータを格納。
         
     | 
| 
      
 88 
     | 
    
         
            +
                        intent.putExtra("menuName", menuName)
         
     | 
| 
      
 89 
     | 
    
         
            +
                        intent.putExtra("menuPrice", menuPrice)
         
     | 
| 
      
 90 
     | 
    
         
            +
                        //第2画面の起動。
         
     | 
| 
      
 91 
     | 
    
         
            +
                        startActivity(intent)
         
     | 
| 
      
 92 
     | 
    
         
            +
                    }
         
     | 
| 
      
 93 
     | 
    
         
            +
                }
         
     | 
| 
      
 94 
     | 
    
         
            +
             
     | 
| 
      
 95 
     | 
    
         
            +
            }
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
            【MenuThanksActivity.kt】
         
     | 
| 
      
 98 
     | 
    
         
            +
            package com.example.kenta.intentsample
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
            import androidx.appcompat.app.AppCompatActivity
         
     | 
| 
      
 101 
     | 
    
         
            +
            import android.os.Bundle
         
     | 
| 
      
 102 
     | 
    
         
            +
            import android.view.View
         
     | 
| 
      
 103 
     | 
    
         
            +
            import android.widget.TextView
         
     | 
| 
      
 104 
     | 
    
         
            +
             
     | 
| 
      
 105 
     | 
    
         
            +
            class MenuThanksActivity : AppCompatActivity() {
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
                override fun onCreate(savedInstanceState: Bundle?) {
         
     | 
| 
      
 108 
     | 
    
         
            +
                    super.onCreate(savedInstanceState)
         
     | 
| 
      
 109 
     | 
    
         
            +
                    setContentView(R.layout.activity_menu_thanks)
         
     | 
| 
      
 110 
     | 
    
         
            +
             
     | 
| 
      
 111 
     | 
    
         
            +
                    //リスト画面から渡されたデータを取得。
         
     | 
| 
      
 112 
     | 
    
         
            +
                    val menuName = intent.getStringExtra("menuName")
         
     | 
| 
      
 113 
     | 
    
         
            +
                    val menuPrice = intent.getStringExtra("menuPrice")
         
     | 
| 
      
 114 
     | 
    
         
            +
             
     | 
| 
      
 115 
     | 
    
         
            +
                    //定食名と金額を表示させるTextViewを取得。
         
     | 
| 
      
 116 
     | 
    
         
            +
                    val tvMenuName = findViewById<TextView>(R.id.tvMenuName)
         
     | 
| 
      
 117 
     | 
    
         
            +
                    val tvMenuPrice = findViewById<TextView>(R.id.tvMenuPrice)
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
                    //TextViewに定食名と金額を表示。
         
     | 
| 
      
 120 
     | 
    
         
            +
                    tvMenuName.text = menuName
         
     | 
| 
      
 121 
     | 
    
         
            +
                    tvMenuPrice.text = menuPrice
         
     | 
| 
      
 122 
     | 
    
         
            +
                }
         
     | 
| 
      
 123 
     | 
    
         
            +
             
     | 
| 
      
 124 
     | 
    
         
            +
                //戻るボタンをタップした時の処理。
         
     | 
| 
      
 125 
     | 
    
         
            +
                fun onBackButtonClick(view: View){
         
     | 
| 
      
 126 
     | 
    
         
            +
                    finish()
         
     | 
| 
      
 127 
     | 
    
         
            +
                }
         
     | 
| 
      
 128 
     | 
    
         
            +
            }
         
     | 
| 
      
 129 
     | 
    
         
            +
             
     | 
| 
      
 130 
     | 
    
         
            +
            【activity_main.xml】
         
     | 
| 
      
 131 
     | 
    
         
            +
            <?xml version="1.0" encoding="utf-8"?>
         
     | 
| 
      
 132 
     | 
    
         
            +
            <ListView
         
     | 
| 
      
 133 
     | 
    
         
            +
                xmlns:android="http://schemas.android.com/apk/res/android"
         
     | 
| 
      
 134 
     | 
    
         
            +
                android:id="@+id/lvMenu"
         
     | 
| 
      
 135 
     | 
    
         
            +
                android:layout_width="match_parent"
         
     | 
| 
      
 136 
     | 
    
         
            +
                android:layout_height="match_parent"/>
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
             
     | 
| 
      
 139 
     | 
    
         
            +
            【activity_menu_thanks.xml】
         
     | 
| 
      
 140 
     | 
    
         
            +
            <?xml version="1.0" encoding="utf-8"?>
         
     | 
| 
      
 141 
     | 
    
         
            +
            <LinearLayout
         
     | 
| 
      
 142 
     | 
    
         
            +
                xmlns:android="http://schemas.android.com/apk/res/android"
         
     | 
| 
      
 143 
     | 
    
         
            +
                android:layout_width="match_parent"
         
     | 
| 
      
 144 
     | 
    
         
            +
                android:layout_height="match_parent"
         
     | 
| 
      
 145 
     | 
    
         
            +
                android:orientation="vertical">
         
     | 
| 
      
 146 
     | 
    
         
            +
             
     | 
| 
      
 147 
     | 
    
         
            +
                <TextView
         
     | 
| 
      
 148 
     | 
    
         
            +
                    android:layout_width="match_parent"
         
     | 
| 
      
 149 
     | 
    
         
            +
                    android:layout_height="wrap_content"
         
     | 
| 
      
 150 
     | 
    
         
            +
                    android:layout_marginBottom="10dp"
         
     | 
| 
      
 151 
     | 
    
         
            +
                    android:layout_gravity="center"
         
     | 
| 
      
 152 
     | 
    
         
            +
                    android:text="@string/tv_thx_title"
         
     | 
| 
      
 153 
     | 
    
         
            +
                    android:textSize="25sp"/>
         
     | 
| 
      
 154 
     | 
    
         
            +
                <TextView
         
     | 
| 
      
 155 
     | 
    
         
            +
                    android:layout_width="match_parent"
         
     | 
| 
      
 156 
     | 
    
         
            +
                    android:layout_height="wrap_content"
         
     | 
| 
      
 157 
     | 
    
         
            +
                    android:layout_marginBottom="10dp"
         
     | 
| 
      
 158 
     | 
    
         
            +
                    android:text="@string/tv_thx_desc"
         
     | 
| 
      
 159 
     | 
    
         
            +
                    android:textSize="15sp"/>
         
     | 
| 
      
 160 
     | 
    
         
            +
                <LinearLayout
         
     | 
| 
      
 161 
     | 
    
         
            +
                    android:layout_width="match_parent"
         
     | 
| 
      
 162 
     | 
    
         
            +
                    android:layout_height="wrap_content"
         
     | 
| 
      
 163 
     | 
    
         
            +
                    android:orientation="horizontal">
         
     | 
| 
      
 164 
     | 
    
         
            +
                    <TextView
         
     | 
| 
      
 165 
     | 
    
         
            +
                        android:id="@+id/tvMenuName"
         
     | 
| 
      
 166 
     | 
    
         
            +
                        android:layout_width="0dp"
         
     | 
| 
      
 167 
     | 
    
         
            +
                        android:layout_height="wrap_content"
         
     | 
| 
      
 168 
     | 
    
         
            +
                        android:layout_weight="1"/>
         
     | 
| 
      
 169 
     | 
    
         
            +
                    <TextView
         
     | 
| 
      
 170 
     | 
    
         
            +
                        android:id="@+id/tvMenuPrice"
         
     | 
| 
      
 171 
     | 
    
         
            +
                        android:layout_width="wrap_content"
         
     | 
| 
      
 172 
     | 
    
         
            +
                        android:layout_height="wrap_content"/>
         
     | 
| 
      
 173 
     | 
    
         
            +
                </LinearLayout>
         
     | 
| 
      
 174 
     | 
    
         
            +
                <Button
         
     | 
| 
      
 175 
     | 
    
         
            +
                    android:layout_width="match_parent"
         
     | 
| 
      
 176 
     | 
    
         
            +
                    android:layout_height="wrap_content"
         
     | 
| 
      
 177 
     | 
    
         
            +
                    android:onClick="onBackButtonClick"
         
     | 
| 
      
 178 
     | 
    
         
            +
                    android:text="@string/bt_thx_back"/>
         
     | 
| 
      
 179 
     | 
    
         
            +
             
     | 
| 
      
 180 
     | 
    
         
            +
            </LinearLayout>
         
     | 
| 
      
 181 
     | 
    
         
            +
             
     | 
| 
      
 182 
     | 
    
         
            +
             
     | 
| 
      
 183 
     | 
    
         
            +
             
     | 
| 
       15 
184 
     | 
    
         
             
            ### 試したこと
         
     | 
| 
       16 
185 
     | 
    
         | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
      
 186 
     | 
    
         
            +
            先ほどソースコードを貼り付けたつもりですが、貼り付けできておりませんでした。
         
     | 
| 
      
 187 
     | 
    
         
            +
            申し訳ありません。
         
     | 
| 
       18 
188 
     | 
    
         | 
| 
       19 
189 
     | 
    
         
             
            ### 補足情報(FW/ツールのバージョンなど)
         
     | 
| 
       20 
190 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
             
     | 
| 
      
 191 
     | 
    
         
            +
            Mac版 AndroidStudi3.6.3
         
     |