0

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:

intent launcher codee

this is where I start the activity 'B':

starting 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.

James Z
  • 12,209
  • 10
  • 24
  • 44
hemant borse
  • 13
  • 1
  • 6
  • Debug your app: https://developer.android.com/studio/debug and clarify the problem: "neither the onclick functionality nor the data passing seems to be working." doesn't tell us anything. https://stackoverflow.com/help/how-to-ask – dominicoder Apr 14 '23 at 19:18
  • Also, please don't use images for showing code - use full text, just like you did in your second block. – dominicoder Apr 14 '23 at 19:22

2 Answers2

0

You appear to be trying to return to Activity A by launching it from Activity B.

This will, I believe, start a new activity A, killing the previous Activity A. The new Activity A will have with it's own brand new set of values (including empty Intent Extras), it will also not invoke the handling of the return values as the activity is not returned to.

Instead you should return to Activity A by finishing activity B with the finish method/function AFTER:-

  • a) adding the value(s) to be returned, to the Intent as Intent Extra(s) and
  • b) setting the appropriate result code

You may find How to manage startActivityForResult on Android useful

MikeT
  • 51,415
  • 16
  • 49
  • 68
0

I belive what you can do is to use finish() to end your activity B. But before that you can use

val intent = Intent()
intent.putExtra("yourKey", yourData)
setResult(RESULT_OK, intent)
finish()

I believe the solution was used in the same way in this tutorial:

https://www.youtube.com/watch?v=DfDj9EadOLk

Hope this helps.

Owlon
  • 3
  • 2