i'm working on some news app, problem is i can't load more news it only shows like 4-5 news from the API first page's and it never shows the older news, help me out to show all the older news from the api please.
maybe the problem from onBindViewHolder? in the Adapter? btw i did it like that in the adapter because i have settings activity that allows the user to choose the news languages since it is an JSON object and the api has no request in the endpoint that is why i did it like that.
thanks in advance.
Here is my main activity:
import android.content.ComponentCallbacks2
import android.content.Context
import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.*
import androidx.navigation.ui.AppBarConfiguration
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import retrofit2.*
import retrofit2.converter.gson.GsonConverterFactory
import android.net.Uri
import android.view.ContextThemeWrapper
import com.nwg.newworlduide.databinding.ActivityDrawerBinding
import java.lang.reflect.Array.get
class DrawerActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityDrawerBinding
var mp: MediaPlayer? = null
var muteBtn: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loadNews()
}
fun loadNews() {
val progress: ProgressBar = findViewById(R.id.pb)
val retrofit = Retrofit
.Builder()
.baseUrl("https://testurl.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
val pref = getSharedPreferences("settings", MODE_PRIVATE)
val source = pref.getString("source", "website-news")
val page = 1.toString()
val callable = retrofit.create(CallableInterface::class.java)
val getNews = callable.getNews(source, page)
getNews.enqueue(object : Callback<List<Article>> {
override fun onResponse(call: Call<List<Article>>, response: Response<List<Article>>) {
progress.visibility = View.GONE
val news = response.body()
val articles = news
showNews(articles)
Log.d("jsoon", articles.toString() )
}
override fun onFailure(call: Call<List<Article>>, t: Throwable) {
progress.visibility = View.GONE
Toast.makeText(this@DrawerActivity, "Failed to show up the data, please check your internet connection and try again later", Toast.LENGTH_SHORT).show();
Log.d("json", "${t.localizedMessage} ")
}
})
}
fun showNews(articles: List<Article>?){
val rv : RecyclerView = findViewById(R.id.rv)
val manager = LinearLayoutManager(this)
rv.layoutManager = manager
val adapter = NewsAdpater(this, articles)
rv.adapter = adapter
rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val lastVisibleItemPosition: Int = manager.findLastVisibleItemPosition()
val totalItemCount = recyclerView!!.layoutManager?.itemCount
super.onScrolled(recyclerView, dx, dy)
if (lastVisibleItemPosition == totalItemCount){
}
}
})
}
here is my interface
interface CallableInterface {
@GET("/api/")
fun getNews(@Query("source") source: String?, @Query("page") page: String?): Call<List<Article>>
}
Here is my adapter
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.core.view.isGone
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
class NewsAdpater(val activity: Activity, val articles: List<Article>?)
: RecyclerView.Adapter<NewsAdpater.NewsViewHolder>() {
class NewsViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val text: TextView = view.findViewById(R.id.tv_news)
val image: ImageView = view.findViewById(R.id.iv_news)
val parent: CardView = view.findViewById(R.id.parent)
val date: TextView = view.findViewById(R.id.date)
val title: TextView = view.findViewById(R.id.title)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
val view = activity.layoutInflater.inflate(R.layout.list_item, parent, false)
return NewsViewHolder(view)
}
override fun onBindViewHolder(holder: NewsAdpater.NewsViewHolder, position: Int) {
val nullTitle = articles?.get(position)?.title
val language = articles?.get(position)?.language
val pref = activity.getSharedPreferences("settings", AppCompatActivity.MODE_PRIVATE)
val source = pref.getString("language", "${null}")
when(source){
"${null}" ->
if(language == null){
holder.text.text = articles?.get(position)?.content
holder.date.text = articles?.get(position)?.posted_at
if (nullTitle != null){
holder.title.text = articles?.get(position)?.title
}
if (nullTitle == null){
holder.title.text = "New Updates"
}
val pictures = articles?.get(position)?.image_url
if (pictures != null){
Glide
.with(activity)
.load(pictures)
.into(holder.image)
}
if (pictures == null){
holder.image.setImageResource(R.drawable.imagedefault)
}
holder.parent.setOnClickListener {
val link = Uri.parse(articles?.get(position)?.source_url)
val intent = Intent(Intent.ACTION_VIEW, link)
activity.startActivity(intent)
}
}
else{
holder.itemView.visibility = View.GONE
holder.itemView.layoutParams = RecyclerView.LayoutParams(0, 0)
}
"FR" ->
if(language == "FR"){
holder.text.text = articles?.get(position)?.content
holder.date.text = articles?.get(position)?.posted_at
if (nullTitle != null){
holder.title.text = articles?.get(position)?.title
}
if (nullTitle == null){
holder.title.text = "Nouvelles mises à jour"
}
val pictures = articles?.get(position)?.image_url
if (pictures != null){
Glide
.with(activity)
.load(pictures)
.into(holder.image)
}
if (pictures == null){
holder.image.setImageResource(R.drawable.imagedefault)
}
holder.parent.setOnClickListener {
val link = Uri.parse(articles?.get(position)?.source_url)
val intent = Intent(Intent.ACTION_VIEW, link)
activity.startActivity(intent)
}
}
else{
holder.itemView.visibility = View.GONE
holder.itemView.layoutParams = RecyclerView.LayoutParams(0, 0)
}
"DE" ->
if(language == "DE"){
holder.text.text = articles?.get(position)?.content
holder.date.text = articles?.get(position)?.posted_at
if (nullTitle != null){
holder.title.text = articles?.get(position)?.title
}
if (nullTitle == null){
holder.title.text = "Neue Updates"
}
val pictures = articles?.get(position)?.image_url
if (pictures != null){
Glide
.with(activity)
.load(pictures)
.into(holder.image)
}
if (pictures == null){
holder.image.setImageResource(R.drawable.imagedefault)
}
holder.parent.setOnClickListener {
val link = Uri.parse(articles?.get(position)?.source_url)
val intent = Intent(Intent.ACTION_VIEW, link)
activity.startActivity(intent)
}
}
else{
holder.itemView.visibility = View.GONE
holder.itemView.layoutParams = RecyclerView.LayoutParams(0, 0)
}
}
}
override fun getItemCount(): Int = articles?.size ?: 0
}
What I have tried:
i tried many methods from the internet and nothin really worked.