【发布时间】:2021-09-26 09:33:04
【问题描述】:
我的片段上有一个 RecyclerView,它从名为 Datasource 的对象中的 ArrayList 获取数据。我想在 Fragment 启动时从 Cloud Firestore 获取新项目,所以我创建了一个名为 getAllProducts 的函数,但是我只能在单击底部导航栏时获取项目。
我几乎尝试了另一个问题的所有答案。我尝试在 onCreate 上重新加载片段,我创建了一个导航动作来片段本身,我在 RecyclerView 适配器中添加了一个从 getAllProducts 函数和 notifyDataSetChanged() 获取数据的函数。我让 getAllProduct 返回数组列表并将其用作 RecyclerView 中的数据。他们都没有工作。
应用启动时:
点击主页时:
这里有相关代码
ProductAdapter.kt
class ProductAdapter(private val productList: ArrayList<ProductItem>):RecyclerView.Adapter<ProductViewHolder>() {
inner class ProductViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
val productImage: ImageView = itemView.findViewById(R.id.productPhoto)
val productName: TextView = itemView.findViewById(R.id.productName)
val productRatingBar: RatingBar = itemView.findViewById(R.id.ratingBar2)
val productRating: TextView = itemView.findViewById(R.id.ratingint)
val storeName: TextView = itemView.findViewById(R.id.productStoreName)
val productPrice: TextView = itemView.findViewById(R.id.productPrice)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.product_item,
parent,false)
return ProductViewHolder(itemView)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val currentItem = productList[position]
holder.itemView.setOnClickListener {
view ->
val action = ProductFragmentDirections.actionProductFragmentToParticularProductFragment2(currentItem)
view.findNavController().navigate(action)
}
holder.productImage.setImageResource(currentItem.productImageResource)
holder.productName.text = currentItem.productName
holder.productRatingBar.rating = currentItem.productRating.toFloat()
holder.productRating.text = currentItem.productRating.toString()
holder.storeName.text = currentItem.storeName
holder.productPrice.text = currentItem.productPrice.toString().plus("$")
}
override fun getItemCount() = productList.size
}
数据源.kt
object Datasource {
var productList = arrayListOf(
ProductItem(R.drawable.attach_money,"Sahibinden aerator",4.5,"Serhat Yilmaz",8),
ProductItem(R.drawable.email,"dus kitapları",3.0,"nerhat Yilmaz",9),
ProductItem(R.drawable.passwordicon,"h files",1.5,"berhat Yilmaz",77),
ProductItem(R.drawable.category,"piyasemen",5.0,"xerhat Yilmaz",55),
ProductItem(R.drawable.attach_money,"Sahibinden sasfaf",1.5,"kerhat Yilmaz",11),
ProductItem(R.drawable.email,"tus kitapları",3.0,"uerhat Yilmaz",21),
ProductItem(R.drawable.passwordicon,"f files",1.5,"yerhat Yilmaz",47),
ProductItem(R.drawable.category,"anguldruva",2.0,"terhat Yilmaz",35),
ProductItem(R.drawable.email,"tus2 kitaplarım",3.0,"werhat Yilmaz",78),
ProductItem(R.drawable.passwordicon,"endobox",2.5,"Zerhat Yilmaz",95),
ProductItem(R.drawable.category,"basplak",4.0,"Ferhat Yilmaz",12)
)
fun loadProduct(): ArrayList<ProductItem>{
return productList
}
}
ProductFragment.kt
class ProductFragment : Fragment() {
private val myDataset = Datasource.productList
private val adapter = ProductAdapter(myDataset)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.products_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView: RecyclerView = view.findViewById(R.id.recycler_product)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireActivity())
FireStoreClass().getAllProducts()
adapter.notifyDataSetChanged()
}
}
FireStoreClass().getAllProducts()
private val mFireStore = FirebaseFirestore.getInstance()
fun getAllProducts() {
mFireStore.collection(Constants.PRODUCTS)
.get()
.addOnSuccessListener {
documents ->
val list = arrayListOf<ProductItem>()
for (document in documents){
val product = document.toObject(ProductItem::class.java)
list.add(product)
}
Datasource.productList = list
}
.addOnFailureListener {
//Toast.makeText(activity, "Can not get Product! ${it.message}", Toast.LENGTH_SHORT).show()
}
}
如果您需要查看:
NavigateActivity.kt
class NavigateActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navigate)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.findNavController()
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavView)
bottomNavigationView.setupWithNavController(navController)
//val action = HomeFragmentDirections
}
}
mobile_navigation.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/mobile_navigation"
app:startDestination="@id/product_Fragment">
<fragment
android:id="@+id/product_Fragment"
android:name="com.example.TheDentalSupplies.fragments.ProductFragment"
android:label="products_fragment"
tools:layout="@layout/products_fragment" >
<action
android:id="@+id/action_productFragment_to_particularProductFragment2"
app:destination="@id/particularProductFragment" />
<action
android:id="@+id/action_productFragment_Reload"
app:destination="@id/product_Fragment" />
</fragment>
<fragment
android:id="@+id/addPostNavActivity"
android:name="com.example.TheDentalSupplies.AddActivity"
android:label="addpost"
tools:layout="@layout/addpost" >
<action
android:id="@+id/action_addPostNavActivity_to_productFragment"
app:destination="@id/product_Fragment" />
</fragment>
<activity
android:id="@+id/userActivity"
android:name="com.example.TheDentalSupplies.UserActivity"
android:label="userlayout"
tools:layout="@layout/userlayout" />
<fragment
android:id="@+id/bucketFragment"
android:name="com.example.TheDentalSupplies.fragments.BucketFragment"
android:label="bucketlayout"
tools:layout="@layout/bucketlayout" />
<fragment
android:id="@+id/particularProductFragment"
android:name="com.example.TheDentalSupplies.fragments.ParticularProductFragment"
android:label="product_layout"
tools:layout="@layout/product_layout">
<argument
android:name="navProductItem"
app:argType="com.example.TheDentalSupplies.models.ProductItem" />
<action
android:id="@+id/action_to_bucketFragment"
app:destination="@id/bucketFragment" />
<action
android:id="@+id/action_to_messageActivity"
app:destination="@id/messageActivity" />
<action
android:id="@+id/action_feedbacksFragment2"
app:destination="@id/feedbacksFragment2" />
</fragment>
<activity
android:id="@+id/messageActivity"
android:name="com.example.TheDentalSupplies.MessageActivity"
android:label="messagelayout"
tools:layout="@layout/messagelayout" />
<fragment
android:id="@+id/feedbacksFragment2"
android:name="com.example.TheDentalSupplies.fragments.FeedbacksFragment"
android:label="feedbacks_layout"
tools:layout="@layout/feedbacks_layout" />
</navigation>
【问题讨论】:
标签: android firebase kotlin google-cloud-firestore android-recyclerview