Reputation: 21
I am working on an app where click on an item in a recycler View opens another activity (Customer-Detail) where there are three Fragments in Tab layout .recycler view contains list of employees, I want to show the details of employee selected from list. I have stored All employee's data in SQLITE in "Employee" Table ( which contains Customer-Id ,Name ,Address ETC) , Now I have Customer-ID (ID of selected Customer) in "Customer-Detail" Activity. I just want to send that "Customer-ID" value to three Fragments of this Customer-Detail Activity. It seems simple but I could not find any solution . Here is my 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>
Here is 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 :
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 }
}
i hope i explained my question well ... i am new here so ... any help will be appreciated
Upvotes: -1
Views: 33
Reputation: 579
use the same viewmodel:
in Activity
:
val viewModel: MyViewModel by viewModels()
in Fragment
:
val viewModel: MyViewModel by activityViewModels()
needs ktx artifcats, see https://developer.android.com/topic/libraries/architecture/viewmodel
Upvotes: 0