【问题标题】:Passing data between data fragments在数据片段之间传递数据
【发布时间】:2020-01-07 05:10:48
【问题描述】:

您好,我制作了一个应用程序并尝试在应用程序的两个片段之间传递数据

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")

但是它不工作它甚至不显示默认文本,我已经应用了所有插件和依赖项

【问题讨论】:

  • 点击后能显示代码吗?
  • 以下答案不起作用,请有人提供更好的方法
  • 请分享您的错误或解释更多!
  • 嘿,我正在应用下面的两个答案,但是当我运行时,我只是在导航,甚至没有显示默认文本。如果您不介意,您能否请一个两屏应用程序,其中数据从片段 1 传递到片段 2,并将其上传到 GitHub 之类的地方,以便我可以比较它。顺便说一句没有编译错误
  • @AshutoshPanda,我更新我的答案,使用它

标签: android android-fragments kotlin navigation


【解决方案1】:

用于传递数据:

val direction = pageoneDirections.actionPageOneToFinalPage("your text")
view?.findNavController()?.navigate(direction)

用于接收数据:

在你的课堂上:

val args: finalpageArgs by navArgs()

在你的 onCreateView 中:

val order_text = args.order_text

【讨论】:

    【解决方案2】:

    将数据change 传递给此的代码:

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

    接收数据change到此的代码:

    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 之类的地方,以便我可以比较它。顺便说一句没有编译错误
    • @AshutoshPanda,你不应用我的答案! ,但你应该调试你的应用程序我的代码是好的我确定,我认为你在片段 2 中获取数据并且你不能在 textView 中显示它也许你在 textView 中的文本颜色与背景颜色相同,请分享你的 xml 代码
    • 我已经检查过文本颜色没有问题,它只是显示 textview。
    • @AshutoshPanda,将你的项目推送到 github 和我的 URL
    • 下载 zip 文件,再次有一个 zip 文件解压并在 android studio 中打开
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 2016-03-25
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    相关资源
    最近更新 更多