Android 在数据片段之间传递数据

Android 在数据片段之间传递数据,android,android-fragments,kotlin,navigation,Android,Android Fragments,Kotlin,Navigation,嗨,我制作了一个应用程序,试图在应用程序中的两个片段之间传递数据 nav_graph.xml <?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.andr

嗨,我制作了一个应用程序,试图在应用程序中的两个片段之间传递数据

nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/title2">
<fragment
    android:id="@+id/title2"
    android:name="com.example.order10.title"
    android:label="title" >
    <action
        android:id="@+id/action_title2_to_pageone"
        app:destination="@id/pageone" />
   </fragment>
   <fragment
    android:id="@+id/pageone"
    android:name="com.example.order10.pageone"
    android:label="pageone" >
    <action
        android:id="@+id/action_pageone_to_finalpage"
        app:destination="@id/finalpage" >
        <argument android:defaultValue="Hi"
            android:name="text" />
    </action>
    </fragment>
    <fragment
    android:id="@+id/finalpage"
    android:name="com.example.order10.finalpage"
    android:label="fragment_finalpage"
    tools:layout="@layout/fragment_finalpage" >
    <argument
        android:name="order_text"
        app:argType="string"
        android:defaultValue="Hi" />
  </fragment>
   <fragment
    android:id="@+id/about"
    android:name="com.example.order10.about"
    android:label="Order 1.0" />
 </navigation>
接收数据的代码

fun navigation(text:String){
    var args= bundleOf()
    args.putString("order_text",text)
    view?.findNavController()?.navigate(R.id.action_pageone_to_finalpage,args)


}      navigation("This is order one ")
 val order_text:TextView?=view?.findViewById(R.id.order_text)
    order_text?.text=arguments?.getString("order_text")

但是它不起作用,它甚至没有显示默认文本,我已经应用了所有插件和依赖项来传递数据:

val direction = pageoneDirections.actionPageOneToFinalPage("your text")
view?.findNavController()?.navigate(direction)
fun navigation(text:String){

    val args = bundleOf("order_text" to text)
    view?.findNavController()?.navigate(R.id.action_pageone_to_finalpage,args)
}
val receiveData = arguments?.getString("order_text")!!

val order_text:TextView?=view?.findViewById(R.id.order_text)
    order_text?.text=receiveData
用于接收数据:

val direction = pageoneDirections.actionPageOneToFinalPage("your text")
view?.findNavController()?.navigate(direction)
fun navigation(text:String){

    val args = bundleOf("order_text" to text)
    view?.findNavController()?.navigate(R.id.action_pageone_to_finalpage,args)
}
val receiveData = arguments?.getString("order_text")!!

val order_text:TextView?=view?.findViewById(R.id.order_text)
    order_text?.text=receiveData
在你们班:

val args: finalpageArgs by navArgs()
在onCreateView中:

val order_text = args.order_text

用于传递数据的代码更改为此:

val direction = pageoneDirections.actionPageOneToFinalPage("your text")
view?.findNavController()?.navigate(direction)
fun navigation(text:String){

    val args = bundleOf("order_text" to text)
    view?.findNavController()?.navigate(R.id.action_pageone_to_finalpage,args)
}
val receiveData = arguments?.getString("order_text")!!

val order_text:TextView?=view?.findViewById(R.id.order_text)
    order_text?.text=receiveData
接收数据的代码更改为:

val direction = pageoneDirections.actionPageOneToFinalPage("your text")
view?.findNavController()?.navigate(direction)
fun navigation(text:String){

    val args = bundleOf("order_text" to text)
    view?.findNavController()?.navigate(R.id.action_pageone_to_finalpage,args)
}
val receiveData = arguments?.getString("order_text")!!

val order_text:TextView?=view?.findViewById(R.id.order_text)
    order_text?.text=receiveData

更新:

val direction = pageoneDirections.actionPageOneToFinalPage("your text")
view?.findNavController()?.navigate(direction)
fun navigation(text:String){

    val args = bundleOf("order_text" to text)
    view?.findNavController()?.navigate(R.id.action_pageone_to_finalpage,args)
}
val receiveData = arguments?.getString("order_text")!!

val order_text:TextView?=view?.findViewById(R.id.order_text)
    order_text?.text=receiveData
将您的
finalPage
更改为:

class finalpage : Fragment() {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        return inflater.inflate(R.layout.fragment_finalpage, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val receiveData = arguments?.getString("order_text")!!
        val order_text:TextView?=view.findViewById(R.id.order_text)
        order_text?.text=receiveData
    }
}

您可以在定义视图后使用组件

单击后是否可以显示代码?以下答案无效。请有人提供更好的方法请分享您的错误或解释更多!!嘿,看,我正在应用下面的两个答案,但当我只运行时,我正在导航,即使默认文本也不会显示。如果你不介意的话,你能不能请一个两屏的应用程序,将数据从片段1传递到片段2,然后上传到GitHub之类的地方,这样我就可以比较了。顺便说一句,没有顺从error@AshutoshPanda,我更新我的答案,使用它们可以看到我正在应用下面的两个答案,但当我只运行时,我正在导航,即使默认文本也不会显示。如果你不介意的话,你能不能请一个两屏的应用程序,将数据从片段1传递到片段2,然后上传到GitHub之类的地方,这样我就可以比较了。顺便说一句,没有顺从error@AshutoshPanda,你不应用我的答案,但是你应该调试你的应用程序我的代码是可以的,我确定,我想你在片段2中得到了数据,但你不能在文本视图中显示它。也许你在文本视图中的文本颜色与背景颜色相同,请共享你的xml代码。我已经检查过文本颜色没有问题,它只是显示文本视图。@AshutoshPanda,在github中推送您的项目并为MedURL下载zip文件,然后再次有一个zip文件将其解压缩并在android studio中打开