2

So I'm doing a project and I'm looking to see if my RecyclerView works. Here's what I got so far

The data class:

@Parcelize
@Entity(tableName = "asteroid_feed")
data class Asteroid(val id: Long, val codename: String, val closeApproachDate: String,
                    val absoluteMagnitude: Double, val estimatedDiameter: Double,
                    val relativeVelocity: Double, val distanceFromEarth: Double,
                    val isPotentiallyHazardous: Boolean) : Parcelable

The Adapter

class AsteroidViewAdapter (private val list: MutableList<Asteroid>) : ListAdapter<Asteroid, AsteroidViewAdapter.AsteroidViewHolder>(DiffCallback) {

   companion object DiffCallback : DiffUtil.ItemCallback<Asteroid>() {
        override fun areItemsTheSame(oldItem: Asteroid, newItem: Asteroid): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Asteroid, newItem: Asteroid): Boolean {
            return oldItem == newItem
        }

    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): AsteroidViewHolder {
        return AsteroidViewHolder(AsteroidListContainerBinding.inflate(LayoutInflater.from(parent.context)))
    }

    override fun onBindViewHolder(holder: AsteroidViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    class AsteroidViewHolder (private val binding: AsteroidListContainerBinding) : RecyclerView.ViewHolder(binding.root) {
         fun bind(item: Asteroid){
            binding.value = item
         }


        }

}

The Fragment

class MainFragment : Fragment() {

    private lateinit var  manager: RecyclerView.LayoutManager

    private val viewModel: MainViewModel by lazy {
        ViewModelProvider(this).get(MainViewModel::class.java)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {


        val binding = FragmentMainBinding.inflate(inflater)
        binding.lifecycleOwner = this

        binding.viewModel = viewModel

        val mutableList: MutableList<Asteroid> = ArrayList()
        mutableList.add(Asteroid(1, "fgnuugrhrg", "bihagtyjerwailgubivb", 4.0, 8.0,3.0, 9.0, false))
        mutableList.add(Asteroid(2, "fguk.nuugrhrg", "bidjswjyhagrwailgubivb", 3.0, 90.0,355.0, 9.0, true))
        mutableList.add(Asteroid(3, "fgnssuugrhrg", "bshjtihagrwailgubivb", 4.0, 33.0,33.0, 9.0, false))
        mutableList.add(Asteroid(4, "fgnuw4suugrhrg", "bjsryjihagrwailgubivb", 6.0, 8.0,11.0, 9.0, true))
        mutableList.add(Asteroid(5, "fgnuugrudkdkhrg", "bihjjkkuagrwailgubivb", 4.0, 5.0,77.0, 9.0, false))

        manager = LinearLayoutManager(this.context)

        binding.asteroidRecycler.apply {
            adapter = AsteroidViewAdapter(mutableList)
            layoutManager = manager
        }


        setHasOptionsMenu(true)

        return binding.root
    }


    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.main_overflow_menu, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return true
    }
}

XML Files: The Layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="viewModel"
            type="com.udacity.asteroidradar.main.MainViewModel" />

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/app_background">

        <FrameLayout
            android:id="@+id/activity_main_image_of_the_day_layout"
            android:layout_width="match_parent"
            android:layout_height="220dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <ImageView
                android:id="@+id/activity_main_image_of_the_day"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                app:srcCompat="@drawable/placeholder_picture_of_day"/>

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:textColor="@android:color/white"
                android:textStyle="bold"
                android:textSize="20sp"
                android:layout_gravity="bottom"
                android:background="#55010613"
                android:text="@string/image_of_the_day" />
        </FrameLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/asteroid_recycler"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/activity_main_image_of_the_day_layout"
            app:layout_constraintVertical_bias="0.0" />

        <ProgressBar
            android:id="@+id/status_loading_wheel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

The Container:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <variable
            name="value"
            type="com.udacity.asteroidradar.Asteroid" />


    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/app_background">

        <TextView
            android:id="@+id/codename"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:textColor="@color/default_text_color"
            android:textSize="20sp"
            android:textStyle="bold"
            android:text="@{value.codename}"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@string/codename" />

        <TextView
            android:id="@+id/date_field"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:textColor="@color/default_text_color"
            android:text="@{value.closeApproachDate}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="@+id/codename"
            app:layout_constraintTop_toBottomOf="@+id/codename"
            tools:text="@tools:sample/date/mmddyy" />

        <ImageView
            android:id="@+id/danger_pic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toEndOf="@+id/codename"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@drawable/ic_status_potentially_hazardous" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

As you can see, I was using a MutableList to fill in the fields and having that list viewed on my RecyclerView.ListAdapter in the fragment_main but it is not showing anything. Again this is just checking to see if The RecyclerView is working. Thanks for the Help.

Zain
  • 37,492
  • 7
  • 60
  • 84
OEThe11
  • 341
  • 2
  • 11

2 Answers2

1

The issue that you send the list to the adapter, but didn't submit it using submitList()

You can do that in init{} of the adapter

class AsteroidViewAdapter(private val list: MutableList<Asteroid>) :
    ListAdapter<Asteroid, AsteroidViewAdapter.AsteroidViewHolder>(DiffCallback) {

    init {
        submitList(list)
    }

// omitted code
Zain
  • 37,492
  • 7
  • 60
  • 84
  • 1
    Wow, your absolutely correct. I even came back to the project and was wondering why the list parameter wasn't even being used in the adapter. Thanks for that. – OEThe11 Jun 19 '21 at 15:23
  • Hey @Zain can you please look at my [issue](https://stackoverflow.com/q/70095578/11560810) – Kotlin Learner Nov 24 '21 at 15:07
0

Based on my understanding list view doesn't know what size of the items in the list to populate. So you need to override item list count by below inside list adapter class.

    override fun getItemCount(): Int {
        return items.size
    }
rasfarrf5
  • 219
  • 1
  • 5