我正试图将数组列表数据传递到主活动导航栏中的一个片段。是否有一个简单的方法来实现这一点?或者我应该使用活动而不是片段?我需要将数组列表数据传递给我的HomeFragment,并在fragment中使用回收器视图适配器,但现在的问题是我不知道如何传递数据,因为我正在使用底部导航视图和Android导航组件。
主要活动package com.example.a5t1v2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.GridView
import androidx.navigation.NavArgument
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
private lateinit var bottomNavigationView: BottomNavigationView
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var shoppingItemList:MutableList<Item>
private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initUI()
val dbHelper = DBHelper(this)
populateDB(dbHelper)
shoppingItemList = dbHelper.getAlItems()
}
private fun populateDB(dbHelper: DBHelper) {
dbHelper.insertItem(Item("Bread",1,"Default", urgent = true, bought = false,null))
dbHelper.insertItem(Item("Chocolate Bar",1,"Small", urgent = false, bought = false,null))
dbHelper.insertItem(Item("Instant noodle",1,"Default", urgent = false, bought = false,null))
dbHelper.insertItem(Item("Juice",2,"Large", urgent = false, bought = false,null))
dbHelper.insertItem(Item("Milk",3,"Large", urgent = true, bought = false,null))
dbHelper.insertItem(Item("Shampoo",1,"Small", urgent = false, bought = true,"24 Aug 2020"))
dbHelper.insertItem(Item("Shower Gel",1,"Large", urgent = false, bought = true,"24 Aug 2020"))
}
private fun initUI() {
appBarConfiguration = AppBarConfiguration(setOf(R.id.urgentListFragment, R.id.homeFragment, R.id.completedListFragment))
bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
navController = findNavController(R.id.fragmentContainerView)
setupActionBarWithNavController(navController,appBarConfiguration)
bottomNavigationView.setupWithNavController(navController)
}
}
一个简单而好的方法是使用sharedViewModel
。
创建ViewModel
并在其中声明LiveData
class MainViewModel: ViewModel () {
private val arrayListLiveData = MutableLiveData<ArrayList<Item>>()
}
在Activity
内部,在活动中创建ViewModel
的instance
,然后您可以在liveData
上使用post
从这里发送arrayList
class MainActivity : AppCompatActivity() {
private lateinit var mainViewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
mainViewModel.arrayListLiveData.post(YOUR_ARRAY_LIST_DATA)
}
}
您可以在fragment
中观察到相同的liveData
,并将从Activity
中接收到发布的arrayList
class TestFragment : Fragment(R.layout.test) {
private lateinit var mainViewModel: MainViewModel
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mainViewModel = ViewModelProvider(requireActivity()).get(MainViewModel::class.java)
mainViewModel.arrayListLiveData.observe(viewLifecycleOwner) { arrayList ->
//Perform desired operation with the array list
}
}
}