【问题标题】:how to send data between activity and its fragments?如何在活动及其片段之间发送数据?
【发布时间】:2021-03-06 09:58:19
【问题描述】:

我正在开发一个应用程序,单击回收站视图中的项目会打开另一个活动(客户详细信息),其中选项卡布局中有三个片段。回收站视图包含员工列表,我想显示员工的详细信息从列表中选择。我已将所有员工的数据存储在 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 }
}

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

【问题讨论】:

标签: android-studio kotlin android-fragments fragmenttransaction supportfragmentmanager


【解决方案1】:

使用相同的视图模型:

Activity:

val viewModel: MyViewModel by viewModels()

Fragment:

val viewModel: MyViewModel by activityViewModels()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多