I just created a simple RecyclerView project which will show 30 basic things . But when i run the file it is not showing anything. In fact my whole source code is just fine.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="12"
tools:listitem="@layout/recycler_view_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.saddyahmed.recyclerview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rv_main.layoutManager = LinearLayoutManager(this)
rv_main.adapter = PostAdapter()
}
}
PostAdapter.kt
package com.saddyahmed.recyclerview
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
class PostAdapter : RecyclerView.Adapter<PostAdapter.PostViewHolder>() {
class PostViewHolder(postView: View) : RecyclerView.ViewHolder(postView){
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
val postView = LayoutInflater.from(parent.context).inflate(R.layout.recycler_view_item , parent ,false)
return PostViewHolder(postView)
}
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
}
override fun getItemCount(): Int {
return 30
}
}
post.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginBottom="5dp"
>
<ImageView
android:id="@+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
tools:src="@drawable/sunny"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Today is Sunny"
android:textSize="20sp"
android:textColor="@android:color/black"
app:layout_constraintTop_toTopOf="@+id/imageView2"
app:layout_constraintStart_toEndOf="@+id/imageView2"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/textView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="285dp"
android:layout_height="20dp"
tools:text="Today is sunny day lets go there"
app:layout_constraintBottom_toBottomOf="@+id/imageView2"
app:layout_constraintStart_toEndOf="@+id/imageView2"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.050" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Actions"
app:layout_constraintTop_toTopOf="@+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
First, add a constructor to your PostAdapter that accepts data and get your data into your PostAdapter, for example:
Then bind it in your
onBindViewHolder:Also
getItemCount()should returnmyDataList.size()