Issue
I'm trying to create a category RecyclerView for the HomeFragment
and I'm having a problem with setting my category and categoryImage in the onDataChange()
function on my addValueEventListener
. How can I do a for loop to get all of my references under my job-category
reference? Thank you!
Database Reference
Category.kt
data class Category constructor(val category: String, val categoryImage: String)
CategoryAdapter.kt
class CategoriesAdapter(val category: ArrayList<Category>) : RecyclerView.Adapter<CategoriesAdapter.ViewHolder>() {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder?.bindCategory(category[position])
}
override fun getItemCount(): Int {
return category.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent?.context).inflate(R.layout.category, parent, false)
return ViewHolder(view)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val categoryName = itemView?.findViewById<TextView>(R.id.categoryNameTextView)
val categoryImage = itemView?.findViewById<ImageView>(R.id.categoryImageView)
fun bindCategory(category: Category) {
categoryName?.text = category.category
categoryImage?.setImageURI(category.categoryImage.toUri())
}
}
}
category.xml
<ImageView
android:id="@+id/categoryImageView"
android:layout_width="420dp"
android:layout_height="676dp"
android:background="@drawable/image_rounded_top_corners"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/profile_photo_"
android:contentDescription="@string/category_image" />
<TextView
android:id="@+id/categoryNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/category_name"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/categoryImageView" />
HomeFragment.kt
lateinit var categoriesAdapter: CategoriesAdapter
val categories = ArrayList Category ()
val categoriesDatabaseRef = FirebaseDatabase.getInstance().reference.child(REF_JOB_CATEGORIES)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_home, container, false)
categoriesAdapter = CategoriesAdapter(categories)
val popularCategoriesRecyclerView = view.findViewById<RecyclerView>(R.id.popularCategoriesRecyclerView)
val layoutManager = GridLayoutManager(view.context, 3, GridLayoutManager.HORIZONTAL, false)
popularCategoriesRecyclerView.layoutManager = layoutManager
categoriesDatabaseRef.addValueEventListener(object: ValueEventListener {
@SuppressLint("NotifyDataSetChanged")
override fun onDataChange(snapshot: DataSnapshot) {
categories.clear()
for (snap in snapshot.children) {
val category = Category(snap.child("category").getValue(String::class.java)!!,snap.child("categoryImage").getValue(String::class.java)!!)
categories.add(category)
}
categoriesAdapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
Log.d("HomeFragment", "LoadPost:onCancelled", error.toException())
}
})
return view
}
Logcat Errors
Process: com.zwdalpha.skedaddle, PID: 19898
java.lang.IllegalArgumentException: Span count should be at least 1. Provided 0
at androidx.recyclerview.widget.GridLayoutManager.setSpanCount(GridLayoutManager.java:829)
at androidx.recyclerview.widget.GridLayoutManager.<init>(GridLayoutManager.java:86)
at com.zwdalpha.skedaddle.Fragments.HomeFragment.onCreateView(HomeFragment.kt:74)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at com.zwdalpha.skedaddle.MainActivity.onStart(MainActivity.kt:36)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
at android.app.Activity.performStart(Activity.java:7157)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Solution
Just Put this code after categories.clear()
for (snap in snapshot.children){
val category = snap.getValue(Category::class.java)
//Here is Other Option To create category object without creating no-arg con
//val category = Category(snap.child("category").getValue(String::class.java)!!,snap.child("categoryImage").getValue(String::class.java)!!)
categories.add(category)
}
You can make list like this
Answered By - gulab patel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.