CustomAdapterのpositionをSentDailyReportsListActivityからSentDailyReportsDetailActivityに遷移後に取得して、一覧で選択したものを詳細表示させたいですが、putExtraで渡せておらずnullになってしまいます。
kotlin
1package com.example.rworksample00026.ui.dailyreports 2 3import android.content.Intent 4import androidx.appcompat.app.AppCompatActivity 5import android.os.Bundle 6import android.util.Log 7import android.view.View 8import androidx.activity.viewModels 9import androidx.recyclerview.widget.DividerItemDecoration 10import androidx.recyclerview.widget.LinearLayoutManager 11import com.example.rworksample00026.databinding.ActivitySentDailyReportsListBinding 12import com.example.rworksample00026.model.entity.DailyReports 13import com.example.rworksample00026.ui.util.CustomAdapter 14 15class SentDailyReportsListActivity : AppCompatActivity() { 16 17 private lateinit var binding : ActivitySentDailyReportsListBinding 18 private val viewModel: DailyReportsViewModel by viewModels() 19 lateinit var mAdapter: CustomAdapter 20 21 override fun onCreate(savedInstanceState: Bundle?) { 22 super.onCreate(savedInstanceState) 23 binding = ActivitySentDailyReportsListBinding.inflate(layoutInflater) 24 setContentView(binding.root) 25 26 // RecyclerViewの取得 27 //https://hirauchi-genta.com/kotlin-recyclerview/ 28 val recyclerView = binding.recyclerView 29 30 val dividerItemDecoration = DividerItemDecoration(this, LinearLayoutManager(this).orientation) 31 recyclerView.addItemDecoration(dividerItemDecoration) 32 33 // LayoutManagerの設定 34 recyclerView.layoutManager = LinearLayoutManager(this) 35 36 // CustomAdapterの生成と設定 37 mAdapter = CustomAdapter(viewModel.reportsData) 38 39 recyclerView.adapter = mAdapter 40 41 mAdapter.setOnItemClickListener(object : CustomAdapter.OnItemClickListener{ 42 override fun onItemClickListener(view: View, position: Int, report: DailyReports) { 43 val intent = Intent(this@SentDailyReportsListActivity, SentDailyReportsDetailActivity::class.java) 44 intent.putExtra("position", position) 45 //Log.d("[position]", position.toString()) 46 startActivity(intent) 47 } 48 }) 49 50 51 52 53 54 } 55 56}
kotlin
1package com.example.rworksample00026.ui.util 2 3import android.view.LayoutInflater 4import android.view.View 5import android.view.ViewGroup 6import android.widget.TextView 7import androidx.recyclerview.widget.RecyclerView 8import com.example.rworksample00026.R 9import com.example.rworksample00026.databinding.ListBinding 10import com.example.rworksample00026.model.entity.DailyReports 11 12 13 14class CustomAdapter (private val dailyReportsList: List<DailyReports>): RecyclerView.Adapter<CustomAdapter.ViewHolder>() { 15 16 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { 17 // create a new view 18 val view = LayoutInflater.from(parent.context).inflate(R.layout.list, parent, false) 19 //view.setOnClickListener(ItemClickListener()) 20 return ViewHolder(view) 21 } 22 23 var point : Int = 0 24 25 // リスナー格納変数 26 lateinit var listener: OnItemClickListener 27 28 // インターフェースの作成 29 interface OnItemClickListener{ 30 fun onItemClickListener(view: View, position: Int) 31 } 32 33 // リスナー 34 fun setOnItemClickListener( listener: OnItemClickListener){ 35 this.listener = listener 36 } 37 /* 38 inner class ItemClickListener: View.OnClickListener{ 39 override fun onClick(v: View) { 40 var context = v.context 41 val intent = Intent(context, SentDailyReportsDetailActivity::class.java) 42 intent.putExtra("point", point) 43 Log.d("[point]", point.toString()) 44 context.startActivity(intent) 45 } 46 } 47 */ 48 49 override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) { 50 /* 51 * https://qiita.com/ngsw_taro/items/d29e3080d9fc8a38691e 52 * withはletとは異なり拡張関数ではありません。 53 * 第一引数に任意の型Tを取ります。 54 * 第二引数に関数を取りますが、Tをレシーバとするメソッドである必要があります。 55 * 56 */ 57 // タップしたとき 58 holder.itemView.setOnClickListener { 59 listener.onItemClickListener(it, position) 60 } 61 62 with(holder){ 63 /* 64 * 65 * positionは項目の位置 66 * https://hirauchi-genta.com/kotlin-listview/ 67 */ 68 69 70 val reports = dailyReportsList[position] 71 //https://stackoverflow.com/questions/60491966/how-to-do-latest-jetpack-view-binding-in-adapter-bind-the-views/60492228 72 // AdapterでのViewBindingの使い方 73 binding.name.text = reports.title 74 binding.body.text = reports.impressions 75 76 77 78 } 79 //this.point = position 80 81 } 82 83 override fun getItemCount(): Int = dailyReportsList.size 84 85 class ViewHolder(view: View): RecyclerView.ViewHolder(view){ 86 val binding = ListBinding.bind(view) 87 val name: TextView 88 val body: TextView 89 90 91 92 init { 93 name = binding.name 94 body = binding.body 95 96 } 97 } 98}
kotlin
1package com.example.rworksample00026.ui.dailyreports 2 3import android.content.Intent 4import android.os.Bundle 5import android.util.Log 6import android.view.View 7import androidx.activity.viewModels 8import com.example.rworksample00026.databinding.ActivitySentDailyReportsDetailBinding 9import com.example.rworksample00026.ui.util.ScopedAppActivity 10import kotlinx.coroutines.Dispatchers 11import kotlinx.coroutines.launch 12 13class SentDailyReportsDetailActivity : ScopedAppActivity() { 14 15 private lateinit var binding : ActivitySentDailyReportsDetailBinding 16 private val viewModel: DailyReportsViewModel by viewModels() 17 18 19 override fun onCreate(savedInstanceState: Bundle?) { 20 super.onCreate(savedInstanceState) 21 binding = ActivitySentDailyReportsDetailBinding.inflate(layoutInflater) 22 setContentView(binding.root) 23 24 25 26 val intent = getIntent() 27 val point = intent.getStringExtra("position") 28 Log.d("[position]", point) 29 val position = point.toInt() 30 31 // 非同期で入力値を表示(1件だけ) 32 launch (Dispatchers.IO){ 33 val data = viewModel.reportsData 34 35 launch (Dispatchers.Main){ 36 binding.title.setText(data.map { it.title }[position].toString()) 37 binding.weeklyGoal.setText(data.map { it.weeklyGoal }[position].toString() ) 38 binding.commutingTimeBefore.setText(data.map { it.commutingTimeBefore }[position].toString() ) 39 binding.commutingTimeAfter.setText(data.map { it.commutingTimeAfter }[position].toString() ) 40 binding.participationProgram.setText(data.map{it.participationProgram}[position].toString()) 41 binding.detailsOfEfforts.setText(data.map{it.detailsOfEfforts}[position].toString()) 42 binding.impressions.setText(data.map{it.impressions}[position].toString()) 43 binding.meal.setText(data.map{it.meal}[position].toString()) 44 binding.sleep.setText(data.map{it.sleep}[position].toString()) 45 binding.motion.setText(data.map{it.motion}[position].toString()) 46 binding.tirednessAndStress.setText(data.map{it.tirednessAndStress}[position].toString()) 47 binding.remarks.setText(data.map{it.remarks}[position].toString()) 48 binding.lookingBack.setText(data.map{it.lookingBack}[position].toString()) 49 binding.nextWeekChallengeAndGoals.setText(data.map{it.nextWeeksChallengeAndGoals}[position].toString()) 50 } 51 } 52 53 binding.Btn.setOnClickListener(object: View.OnClickListener{ 54 override fun onClick(v: View?) { 55 val intent = Intent(this@SentDailyReportsDetailActivity, SentDailyReportsListActivity::class.java) 56 startActivity(intent) 57 } 58 }) 59 } 60}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。