4
3

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 3 years have passed since last update.

SimpleAdapterにより生成されたデータがタップされた時に画面遷移する(ように見せるインテント)

Posted at

概要

画面は、activity_main.xml->activity_thanks.xmlに遷移します。

SimpleAdapterを作る

activity_main.xml
    <ListView
        android:id="@+id/lvMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
MainActivity.kt
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val lvMenu = findViewById<ListView>(R.id.lvMenu)  //★ListView取得
        val menuList: MutableList<MutableMap<String,String>> = mutableListOf()

        var menu = mutableMapOf("name" to "Aランチ","price" to "800円")
        menuList.add(menu)
        menu = mutableMapOf("name" to "Bランチ","price" to "1000円")
        menuList.add(menu)
        menu = mutableMapOf("name" to "Cランチ","price" to "900円")
        menuList.add(menu)

        val from = arrayOf("name","price")
        val to = intArrayOf(android.R.id.text1,android.R.id.text2)

        val adapter = SimpleAdapter(applicationContext,menuList,android.R.layout.simple_list_item_2,from,to)
        lvMenu.adapter = adapter

リストタップのリスナクラスを作る

MainActivity.kt
//~~~~~~~~~~省略~~~~~~~~~~~~
        val adapter = SimpleAdapter(applicationContext,menuList,android.R.layout.simple_list_item_2,from,to)
        lvMenu.adapter = adapter

        lvMenu.onItemClickListener = ListItemClickListener()

    }

    private inner class ListItemClickListener : AdapterView.OnItemClickListener{
        override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            val item = parent?.getItemAtPosition(position) as MutableMap<String, String>
            val menuName = item["name"]
            val menuPrice = item["price"]

           // インテントオブジェクト
            val intent = Intent(applicationContext, MenuThanksActivity::class.java)
           // 送るデータ
            intent.putExtra("menuName", menuName)
            intent.putExtra("menuPrice", menuPrice)

            startActivity(intent)
        }
    }

遷移先の画面に処理をかく

xmlに吐き出すようのタグを入れておきます

activity_thanks.xml
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvMenuName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
            <TextView
                android:id="@+id/tvMenuPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
ThanksActivity.kt
class MenuThanksActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_menu_thanks)

        //インテントでもらったデータ
        val menuName = intent.getStringExtra("menuName") 
        val menuPrice = intent.getStringExtra("menuPrice")
        // idに値を渡す
        tvMenuName.text = menuName
        tvMenuPrice.text = menuPrice
    }
    // 戻るボタンの処理
    fun onBackButtonClick(view: View) {
        finish()
    }
}

これで表示される。
画面遷移と言うよりも表示されているリストの上に新しく表示を重ねているだけ。
だからfinish()でその表示が消えると言うのがポイント〜

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?