Recycler View is having overwritten elements when I make a new data request

Asked

Viewed 39 times

0

I’m having trouble requisitioning and putting the results in Recycler View. Whenever I make a new request Recycler View has the elements replaced instead of preserving what has already been called and just add more elements in the list.

My requisition code is like this:

class Api (val recyclerView: RecyclerView) : MainActivity(){
    private lateinit var adapter: Adapter
    private val remote3 = Connection.createService(Repository::class.java)

    fun getResponsesFromRepository(page: Int) {
        val callPostRepository: Call<Domain.Items> = remote3.list("language:Java", "stars", page)

        callPostRepository.enqueue(object : Callback<Domain.Items> {
            override fun onFailure(call: Call<Domain.Items>, t: Throwable) {
                Toast.makeText(baseContext, t.message, Toast.LENGTH_LONG).show()
                println(t.message)
            }

            override fun onResponse(
                call: Call<Domain.Items>,
                response: Response<Domain.Items>
            ) {
                if (response.isSuccessful) {
                    var repo: Domain.Items = response.body()!!
                    recyclerView.layoutManager = LinearLayoutManager(instance)
                    adapter = Adapter(repo)
                    recyclerView.adapter = adapter
                }
            }
        })
    }

The Main Activity code looks like this:

open class MainActivity : AppCompatActivity() {

    private var count = -1
    private lateinit var recycler: RecyclerView
    private lateinit var api: Api
    private lateinit var loading: View

    companion object {
        lateinit var instance: MainActivity
            private set
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        instance = this
        recycler = findViewById(R.id.rec)
        api = Api(recycler)
        loading = findViewById(R.id.frame_loading)

        recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)

                if (!recyclerView.canScrollVertically(1) && newState == RecyclerView.SCROLL_STATE_IDLE) {
                    fetchNewData(api, loading)
                }
            }
        })

    fun fetchNewData(api: Api, view: View) {
        api.getResponsesFromRepository(count)
        count++
    }
}

Whenever I go to the end of the page it carries more elements, but it does not add these elements in the view Recycler and rather replace those that have already been loaded, if someone give me a light on how to solve this I thank kkkkkkk

1 answer

0

What you can do:

 private lateinit var adapter: Adapter
 private val remote3 = Connection.createService(Repository::class.java)
 private val listaDeAlgumaCoisa = arrayListOf<Item>()


...

if (response.isSuccessful) {
                    var repo: Domain.Items = response.body()!!
                    recyclerView.layoutManager = LinearLayoutManager(instance)
                    listaDeAlgumaCoisa.addAll(repo)
                    adapter = Adapter(listaDeAlgumaCoisa)
                    recyclerView.adapter = adapter
                }

However, what Voce should do is create a class that inherits Adapter() and has a function inside that is to add more items. I have an example in my first pokedex on android

Browser other questions tagged

You are not signed in. Login or sign up in order to post.