当前位置: 首页 > 知识库问答 >
问题:

为什么我不能从DialogFragment到Fragment接收数据?

冯良才
2023-03-14

我正在尝试将数据从DialogFraank发送到创建此对话框的Fraank。在目标片段中,我使用以下代码:

val noteData = NoteData()
val bundle = Bundle()
noteData.setTargetFragment(this,0)
noteData.show(fragmentManager!!, noteData.TAG)

我还在fragment实现了我的接口:

class NotepadScr : Fragment(), NotesInterface

在DialogFragment中,我使用以下代码:

private var notesInterface: NotesInterface? = null
 override fun onAttach(context: Context) {
        super.onAttach(context)

        if (context is NotesInterface) {
            notesInterface = context
        } else if (parentFragment is NotesInterface) {
            notesInterface = parentFragment as NotesInterface
        }
    }

并发送数据:

notesInterface.archiveNote(recordID!!)

我还试图从这个问题中使用onActivityResult():

val i = Intent()
i.putExtra("selectedDate", 1122)
targetFragment!!.onActivityResult(1, Activity.RESULT_OK, i)

接收数据:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when(requestCode){
            1->{
                if (resultCode == Activity.RESULT_OK) {
                    // here the part where I get my selected date from the saved variable in the intent and the displaying it.
                    val bundle = data!!.extras
                    val resultDate = bundle!!.getInt("selectedDate")
                    Timber.i("****************************   %s", resultDate.toString())
                }
            }
        }
    }

但我没有收到数据:(((

共有1个答案

方建明
2023-03-14

数据必须按如下所示发送:

ReceivingFragment receivingFragment = new ReceivingFragment
Bundle bundle = new Bundle()
bundle.putString(KEY, value to be sent);//Use Int if you want to send int value
receivingFragment.setArguments(bundle);

这必须在接收碎片时完成

String receivingData = getArguments.getString(KEY);

确保KEY在发送片段和接收片段中相同。

 类似资料:
  • 我的要求: 发现错误: 在这一行中..为什么值不显示在mainfragment中??

  • 我正在尝试使用python和MySQL开发药房管理系统。但我在连接数据库时遇到了问题。 下面是一些代码:

  • 我使用DialogFragments做很多事情:从列表中选择项目,输入文本。 谢谢你的帮助。

  • 我正在尝试使用python和MySQL开发药房管理系统。但我在连接数据库时遇到了问题。 下面是一些代码:

  • 问题内容: 我在android虚拟机中使用以下代码 我收到HttpHostConnectException。不知道为什么?我已将网址中的地址从127.0.0.1更改为10.0.2.2,但仍然收到该异常。我的电脑中安装了wamp服务器,文件“ ReadingFromServer.php”位于“ www”文件夹中。 这是完整的堆栈跟踪 谢谢。 问题答案: 您是否在AndroidManifest.xml

  • 我正在尝试用我用给定的主机名设置的用户名和密码进行连接。我无法连接。我已经检查了安全组在端口的配置是否正确,允许从“我的IP”传入,它在那里填充了我的IP。 我还会做错什么? 当我尝试在终端中使用以下命令进入DB时: 我得到:上的MySQL服务器