如何在 Activity 及其片段之间发送数据?

问题描述

我正在开发一个应用程序,点击回收者视图中的一个项目会打开另一个活动(客户详细信息),其中选项卡布局中有三个片段。回收者视图包含员工列表,我想显示员工的详细信息从列表中选择。我已将 sqlITE 中的所有员工数据存储在“员工”表(其中包含客户 ID、姓名、地址等)中,现在我在“客户详细信息”活动中有客户 ID(所选客户的 ID)。我只想将“客户 ID”值发送到此客户详细信息活动的三个片段。看起来很简单,但我找不到任何解决方案。这是我的 CustomerDetail.xml:

<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CustomerDetail">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/custID"
        />

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabInlineLabel="true"
            app:tabBackground="@color/lightRed"
            app:tabIndicatorColor="@color/white"
            app:tabTextColor="@color/white"
            app:tabMode="fixed"
            android:id="@+id/tablayout"/>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appbar"
        android:id="@+id/viewPager"
        />

</RelativeLayout>

这是 CustomerDetails.kt :

class CustomerDetail : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_customer_detail)
        val extras: Bundle? = intent.extras
        val EmployeeID: Int = extras!!.getInt("id")
        
        sendToFragmentOrder(EmployeeID)
        
        val appbar = findViewById<AppBarLayout>(R.id.appbar)
        val tabLayout = findViewById<TabLayout>(R.id.tablayout)
        val viewPager = findViewById<ViewPager>(R.id.viewPager)
        setUpTabs()
    }

    private fun sendToFragmentOrder(id : Int) {
        // this method is not working...
        val bundle = Bundle()
        bundle.putInt("id",id)
        val fragmentOrder = OrderFragment()
        fragmentOrder.arguments = bundle
    }

    private fun setUpTabs() {
        val adapter = mPagerAdapter(supportFragmentManager)
        adapter.addFragment(OrderFragment(),"Orders")
        adapter.addFragment(ReceiptsFragment(),"Receipts")
        adapter.addFragment(ReportsFragment(),"Reports")
        val viewpager = findViewById<ViewPager>(R.id.viewPager)
        viewpager.adapter = adapter
        val tab = findViewById<TabLayout>(R.id.tablayout)
        tab.setupWithViewPager(viewpager)
    }
}

OrderFragment() 类:

class OrderFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
    override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {

        val id = arguments?.getInt("id")

        val view=inflater.inflate(R.layout.fragment_order,container,false)
        val tv = view.findViewById<TextView>(R.id.idtv)
        tv.text = id.toString()
        return  view }
}

我希望我能很好地解释我的问题......我是新来的所以......任何帮助将不胜感激

解决方法

使用相同的视图模型:

Activity中:

val viewModel: MyViewModel by viewModels()

Fragment中:

val viewModel: MyViewModel by activityViewModels()

需要 ktx artifcats,见 https://developer.android.com/topic/libraries/architecture/viewmodel