当活动'B'中的回收视图项被点击时,需要将数据从活动'B'发送到活动'A'(A启动B)

问题描述 投票:0回答:1

我尝试的方法是首先启动活动“B”,然后在活动“B”中尝试为回收视图的项目设置 onclick 侦听器,然后当单击该项目时,我使用意图将数据传递给活动“A”。 (我在启动意图时使用了 startActivityForResult )。 onclick 功能和数据传递似乎都不起作用。

这是意图发射器:

e

这是我开始活动“B”的地方:

这是活动“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
        }
    }
}

这是适配器:

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()
    }

}

请提供一些参考或指导直接解决方案将不胜感激。

android kotlin android-recyclerview android-activity android-room
1个回答
0
投票

您似乎正试图通过从 Activity B 启动 Activity A 来返回它。

我相信,这将启动一个新的 Activity A,杀死之前的 Activity A。新的 Activity A 将拥有自己全新的一组值(包括空的 Intent Extras),它也不会调用返回的处理值,因为活动未返回。

相反,您应该通过使用

finish
方法/函数完成活动 B 来返回活动 A AFTER:-

  • a) 将要返回的值添加到 Intent 作为 Intent Extra(s) 和
  • b) 设置适当的结果代码

您可能会发现How to manage startActivityForResult on Android useful

© www.soinside.com 2019 - 2024. All rights reserved.