I can't view my data from api in recyclerview. Can you help me what is the problem?
MyCode:
Adapter:
class NewsAdapter(private val data: List<AllData>) :
RecyclerView.Adapter<NewsAdapter.MyViewHolder>() {
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val title = view.findViewById<TextView>(R.id.news_info)
val imageView = view.findViewById<ImageView>(R.id.news_img)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.main_activity_cardview_card_menu, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val news:AllData=data[position]
holder.title.text = news.haberler[position].title
Glide.with(holder.imageView.context).load(news.haberler[position].imageUrl).into(holder.imageView)
}
override fun getItemCount(): Int {
return data.size
}
}
activity_main.xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/news_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view" />
MainActivity:
class MainActivity : AppCompatActivity() {
private val itemList = ArrayList<MenuCardModel>()
private lateinit var menuCardAdapter: MenuCardAdapter
private lateinit var newsAdapter: NewsAdapter
val data = ArrayList<AllData>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadNews()
}
private fun loadNews() {
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(ApiInterface::class.java)
val call = service.getNews()
call.enqueue(object :Callback<AllData>{
override fun onResponse(
call: Call<AllData>,
response: Response<AllData>
) {
response.body()?.let {
data.add(it)
newsAdapter = NewsAdapter(data)
news_recyclerView.adapter = newsAdapter
}
}
override fun onFailure(call: Call<AllData>, t: Throwable) {
t.printStackTrace()
}
})
}
}
Model :
data class Haberler(
val content: String,
val id: Int,
val imageUrl: String,
val orderNo: Int,
val parentId: Any,
val title: String,
val videoUrl: String
)
AllData :
data class AllData(
val haberler: List<Haberler>,
val istatistik: Istatistik,
val konferans: Konferans,
val kvkk: String,
val liveChat: String,
val pratikBilgiler: List<PratikBilgiler>,
val sgkTv: List<SgkTv>,
val sss: List<Ss>,
val state: Boolean,
val subjects: List<Subject>
)
Hello, I can't view my data from api in recyclerview. Can you help me what is the problem?
Hello, I can't view my data from api in recyclerview. Can you help me what is the problem?
Hello, I can't view my data from api in recyclerview. Can you help me what is the problem?
It looks like you forgot to set layout manager to your recyclerview before you set adapter
There are two ways you can set layout manager
First way by xml
<androidx.recyclerview.widget.RecyclerView
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
...
/>
second way is by code
newsAdapter = NewsAdapter(data)
news_recyclerView.layoutManager = LinearLayoutManager(context) // line which you forgot
news_recyclerView.adapter = newsAdapter
for more understanding about layout managers , you can refer below links
https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager