Home > Net >  Data not displaying in recycler View
Data not displaying in recycler View

Time:11-16

I'm fairly new to Mobile dev and kotlin in general. Right now, I'm trying to populate a recyclerview with data from the database. Issue is, I know the connection is established because I can see the log message on the console

I am not really sure what I'm doing wrong at this point and I don't even understand anymore where is the issue. I have tried almost every solution I found on here or other websites but nothing worked out.


Adapter :

class restaurantadapter(private var restaurantList: ArrayList<Restaurant>, private val context: Context) : RecyclerView.Adapter<restaurantadapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.restaurant_card_view, parent, false))
    }

    override fun getItemCount(): Int {
        return restaurantList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val restaurant = restaurantList.get(position)
        holder.titleTextView?.text = restaurant.title
        holder.desTextView?.text = restaurant.description
        }
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
         var titleTextView: TextView? = null
         var desTextView: TextView? = null

        init {
            titleTextView = itemView.findViewById(R.id.restaurantname)
            desTextView = itemView.findViewById(R.id.restaurantdesc)

        }
    }

Main activity

class Main1Activity : AppCompatActivity() {

    var restaurantList: ArrayList<Restaurant> = ArrayList()
   private var adapter: restaurantadapter? = null


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

        var recyclerView: RecyclerView = findViewById(R.id.restaurantslist)
        recyclerView.layoutManager= LinearLayoutManager(this@Main1Activity)
        adapter=restaurantadapter(restaurantList,this
        )

        recyclerView.adapter=adapter

      ApiClient.getApiClient()!!.create(ApiInterface::class.java)

        ApiClient.apiservice.getRestaurants()
            .enqueue(object: Callback<ArrayList<Restaurant>>{
                override fun onResponse(
                    call: Call<ArrayList<Restaurant>>,
                    response: Response<ArrayList<Restaurant>>
                )
                {
                   val response = response.body()
                    response?.let {
                        restaurantList.addAll(it)
                        adapter?.notifyDataSetChanged()

                    }
                }

                override fun onFailure(call: Call<ArrayList<Restaurant>>, t: Throwable) {
                    Toast.makeText(this@Main1Activity,"failure",Toast.LENGTH_SHORT).show()
                }
            })



    }

layout :

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_margin="6dp"
    app:contentPadding="12dp"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    >

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp">

        <TextView
            android:id="@ id/restaurantname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:textColor="@color/black"
            android:textSize="22sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_gravity="center"
          />

        <TextView
            android:id="@ id/restaurantdesc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"
            android:layout_marginTop="55dp"
            android:layout_marginEnd="16dp"
            android:textColor="@color/black"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/restaurantname"
            android:textSize="22sp"
             />
        </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

CodePudding user response:

Your problem is that you passed empty list restaurantList to the adapter and then added the response to this list restaurantList.addAll(it) but this will update the list in your activity, not the list in the adapter, to solve this problem you can create method in the adapter to update its list

fun updateData(newRestaurant: ArrayList<Restaurant>) {
    restaurantList = newRestaurant
    notifyDataSetChanged()
}

The code in onResponse will be:

val response = response.body()
response?.let {
    adapter.updateData(it)
}
  • Related