1 year ago

#77319

test-img

Zachary Smouse

Populate Three RecyclerViews on an Activity

I'm trying to populate three RecyclerViews on my ServiceActivity based on the category property in a Firebase Realtime Database reference child. Right now I'm having trouble getting the RecyclerViews to show up on the activity when I run the app. When I try running, I don't get an error in the logcat so I need help in determining if my adapters are in the wrong spot or if I need to reconfigure my fetch data function. Thank you!

What I Have

enter image description here

What I Need

enter image description here

Database Reference

enter image description here

activity_service.xml

<androidx.recyclerview.widget.RecyclerView
     android:id="@+id/autoServicesRecyclerView"
     android:layout_width="0dp"
     android:layout_height="0dp"
     android:layout_marginStart="16dp"
     android:layout_marginTop="16dp"
     android:layout_marginEnd="16dp"
     android:layout_marginBottom="16dp"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toBottomOf="@+id/autoServicesTextView" />

<androidx.recyclerview.widget.RecyclerView
     android:id="@+id/homeServicesRecyclerView"
     android:layout_width="0dp"
     android:layout_height="0dp"
     android:layout_marginStart="16dp"
     android:layout_marginTop="16dp"
     android:layout_marginEnd="16dp"
     android:layout_marginBottom="16dp"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toBottomOf="@+id/homeServicesTextView" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/personalServicesRecyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/personalServicesTextView" />

ServiceActivity.kt

class ServiceActivity : AppCompatActivity() {

    lateinit var autoJobServicesAdapter: JobServicesAdapter
    lateinit var homeJobServicesAdapter: JobServicesAdapter
    lateinit var personalJobServicesAdapter: JobServicesAdapter
    val jobServices = ArrayList<JobService>()
    val jobServicesDatabaseRef = FirebaseDatabase.getInstance().reference.child(REF_JOB_SERVICES)

    val autoServices = ArrayList<JobService>()
    val homeServices = ArrayList<JobService>()
    val personalServices = ArrayList<JobService>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_service)

        setupAdapters()
        loadDataForRecyclerViews()
    }

    private fun setupAdapters() {

        autoJobServicesAdapter = JobServicesAdapter(jobServices)

        val autoRecyclerView = findViewById<RecyclerView>(R.id.autoServicesRecyclerView)
        autoRecyclerView.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = autoJobServicesAdapter
            setHasFixedSize(true)
        }

        homeJobServicesAdapter = JobServicesAdapter(jobServices)

        val homeRecyclerView = findViewById<RecyclerView>(R.id.homeServicesRecyclerView)
        homeRecyclerView.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = homeJobServicesAdapter
            setHasFixedSize(true)
        }

        personalJobServicesAdapter = JobServicesAdapter(jobServices)

        val personalRecyclerView = findViewById<RecyclerView>(R.id.personalServicesRecyclerView)
        personalRecyclerView.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = personalJobServicesAdapter
            setHasFixedSize(true)
        }

    }

    private fun loadDataForRecyclerViews() {

        jobServicesDatabaseRef.orderByChild("jobName").addValueEventListener(object: ValueEventListener {
            @SuppressLint("NotifyDataSetChanged")
            override fun onDataChange(snapshot: DataSnapshot) {
                jobServices.clear()
                for (snap in snapshot.children) {
                    val jobService = JobService(snap.child("category").getValue(String::class.java)!! ,
                    snap.child("jobName").getValue(String::class.java)!! , snap.child("jobImageUrl").getValue(String::class.java)!! ,
                    snap.child("jobServiceImageUrl").getValue(String::class.java)!!)

                    if (jobService.category == "Auto Services") {
                        autoServices.add(jobService)
                    }
                    else if (jobService.category == "Home Services") {
                        homeServices.add(jobService)
                    }
                    else if (jobService.category == "Personal Services") {
                        personalServices.add(jobService)
                    }
                }

                autoJobServicesAdapter.notifyDataSetChanged()
                homeJobServicesAdapter.notifyDataSetChanged()
                personalJobServicesAdapter.notifyDataSetChanged()
            }

            override fun onCancelled(error: DatabaseError) {
                Log.d("ServiceActivity", "LoadJobs", error.toException())
            }
        })
    }

}

JobService.kt

data class JobService constructor(var category: String, val jobName: String, val jobImageUri: String, val jobServiceImageUri: String)

JobServicesAdapter

class JobServicesAdapter(val jobService: ArrayList<JobService>) : RecyclerView.Adapter<JobServicesAdapter.ViewHolder>() {

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder?.bindJobService(jobService[position])
    }

    override fun getItemCount(): Int {
        return jobService.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.jobservicerecyclerview, parent, false)
        return  ViewHolder(view)
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val jobName = itemView?.findViewById<TextView>(R.id.jobServiceLabelTextView)
        val jobServiceImage = itemView?.findViewById<ImageView>(R.id.jobServiceIconImageView)

        fun bindJobService(jobservice: JobService) {

            jobName?.text = jobservice.jobName
            Picasso.get().load(jobservice.jobImageUri).into(jobServiceImage)

        }

    }

}

android

firebase-realtime-database

android-recyclerview

0 Answers

Your Answer

Accepted video resources