在数据片段之间传递数据
Passing data between data fragments
您好,我制作了一个应用程序并尝试在应用程序中的两个片段之间传递数据
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)
接收数据:
在你的 class 中:
val args: finalpageArgs by navArgs()
在你的 onCreateView 中:
val order_text = args.order_text
将数据 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
}
}
您可以在定义视图后使用组件
您好,我制作了一个应用程序并尝试在应用程序中的两个片段之间传递数据
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)
接收数据:
在你的 class 中:
val args: finalpageArgs by navArgs()
在你的 onCreateView 中:
val order_text = args.order_text
将数据 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
}
}
您可以在定义视图后使用组件