The approach I tried is first I start activity 'B' then in activity 'B' I try to set onclick listener for items of Recycle view and then when the item is clicked I use intent to pass data to activity 'A'. (I have used startActivityForResult while starting intent). neither the onclick functionality nor the data passing seems to be working.
this is the intent launcher:
this is where I start the activity 'B':
This is the Activity 'B':
class TaskListActivity : AppCompatActivity() {
private var binding: ActivityTaskListBinding? = null
private var tasks: List<TaskEntity>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityTaskListBinding.inflate(layoutInflater)
setContentView(binding?.root)
// instance of the TaskDao (this consists of all the methods)
val taskdao = (application as TaskApp).db.taskDao()
lifecycleScope.launch {
taskdao.fetchAllTasks().collect {
// Log.d("some task", "$it")
tasks = ArrayList(it)
val list = ArrayList(it)
setupRecycleView(list, taskdao)
}
}
binding?.btnAddTask?.setOnClickListener {
showInputDialog(taskdao)
}
}
private fun showInputDialog(taskdao: TaskDao) {
val dialog = AlertDialog.Builder(this)
val view = LayoutInflater.from(this).inflate(R.layout.add_task_layout, null)
val etTaskDesc: TextInputEditText = view.findViewById(R.id.etTaskDesc)
val npTotalPomos: NumberPicker = view.findViewById(R.id.npTotalPomo)
npTotalPomos.minValue = 0
npTotalPomos.maxValue = 10
npTotalPomos.wrapSelectorWheel = true
dialog
.setMessage("Add Task")
.setView(view)
.setPositiveButton("Add", DialogInterface.OnClickListener { dialogInterface, i ->
val td = etTaskDesc.text.toString()
val tp = npTotalPomos.value
lifecycleScope.launch {
if (td.isNotEmpty()) {
taskdao.insert(TaskEntity(0, td, tp.toString(), "0/"))
} else {
Toast.makeText(
this@TaskListActivity,
"Task description cannot be empty",
Toast.LENGTH_LONG
).show()
}
}
})
.setNegativeButton("Cancel", null).create().show()
}
private fun setupRecycleView(allTasks: ArrayList<TaskEntity>, taskdao: TaskDao) {
if (allTasks.isNotEmpty()) {
val taskAdapter = TaskAdapter(allTasks)
binding?.rvTaskListIntent?.layoutManager = LinearLayoutManager(this)
binding?.rvTaskListIntent?.adapter = taskAdapter
binding?.rvTaskListIntent?.visibility = View.VISIBLE
}
}
}
this is the adapter:
class TaskAdapter(
val allTasks: List<TaskEntity>
) : RecyclerView.Adapter<TaskAdapter.ViewHolder>() {
class ViewHolder(binding: TaskItemBinding) : RecyclerView.ViewHolder(binding.root) {
val llMainContainer = binding.llMainContainer
val taskDesc = binding.tvTaskDesc
val totalPomo = binding.tvTotalPomo
val compPomo = binding.tvCompPomos
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = TaskItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return allTasks.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = allTasks[position]
holder.taskDesc.text = item.task_desc
holder.compPomo.text = item.comp_pomos.toString()
holder.totalPomo.text = item.total_pomos.toString()
}
}
Please provide some references or guides direct solutions would be appreciated.