asked    Monica     2018-10-22       android       74 view        1 Answer

[SOLVED] RecyclerView swap two adapters

I want to make an app which shows the last 15 match and next 15 matches using API. I created a RecyclerView with 2 different list view. One for the last match, and the other for the next match. I've also created 2 different adapters, so when I click the different navigation bar, it will change the view(for example last match) into another view(next match). However, when I click the second button(next match) it force stopped.

I think the problem is in the Adapter set, but I don't know where

Here are my codes:

MainActivity.kt

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.navigation_home -> {
            //Prev Match
            recyclerView_main.layoutManager = LinearLayoutManager(this)
            fetchJsons()
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_dashboard -> {
            //Next Match
            recyclerView_main.layoutManager = LinearLayoutManager(this)
            fetchJson()
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}
fun fetchJsons(){
    //Json for prev match
    val url = "https://www.thesportsdb.com/api/v1/json/1/eventspastleague.php?id=4328"
    val request = Request.Builder().url(url).build()
    val client = OkHttpClient()
    client.newCall(request).enqueue(object: Callback{
        override fun onResponse(call: Call?, response: Response?){
            println("sukses")
            val body = response?.body()?.string()
            println(body)

            val gson = GsonBuilder().create()

            val prevMatch = gson.fromJson(body, PrevMatch::class.java)

            runOnUiThread { recyclerView_main.adapter = MainAdapters(prevMatch) }
        }
        override fun onFailure(call: Call?, e: IOException?){
            println("failed request")
        }
    })
}

// Next Match JSON
fun fetchJson(){
    val url = "https://www.thesportsdb.com/api/v1/json/1/eventsnextleague.php?id=4328"
    val request = Request.Builder().url(url).build()
    val client = OkHttpClient()
    client.newCall(request).enqueue(object: Callback{
        override fun onResponse(call: Call?, response: Response?){
            println("sukses")
            val body = response?.body()?.string()
            println(body)

            val gson = GsonBuilder().create()

            val nextMatch = gson.fromJson(body, NextMatch::class.java)

            runOnUiThread { recyclerView_main.adapter = MainAdapter(nextMatch) }
        }
        override fun onFailure(call: Call?, e: IOException?){
            println("failed request")
        }
    })
}

  1 Answer  

        answered    Patrick     2018-10-22      

Here's what i will do, i will create two recyclerView and switch between them. and divide data from the API between them.

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                //Prev Match
                recyclerView_Next_Match.visibility = View.GONE
                recyclerView_Prev_Match.visibility = View.VISIBLE
                fetchJsons()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                //Next Match
                recyclerView_Prev_Match.visibility = View.GONE
                recyclerView_Next_Match.visibility = View.VISIBLE
                fetchJson()
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }




Your Answer





 2018-10-22         Berg

Eloquent - How to use "Has Many Through" relation?

I have three tables - products, features and product_feature - asproducts - id - namefeatures - id - keyproduct_feature - product_id - feature_id - valueI was retrieve all (key, value) pairs for a product. The SQL statement isSELECT key, value FROM productsJOIN product_feature pfON pf.product_id = "Product ID"JOIN features fON f.id = pf.feature_idHow do I established that relationship// Inside Product modelfunction features() { // Has many through relationship} This is a BelongsToMany relationship:public function features() { return $this->b...
 eloquent                     1 answers                     49 view
 2018-10-22         Jerome

Laravel Many to many relationship with custom key relation

Before you mark this question as duplicate, please see the details :) I have a problem related to many to many relationship with custom columns linkingI have following tables Employees -Id -brink_id <----- This is third party id I am saving of employee -nameJobs-id-brink_id <----- This is third party id I am saving of job-descriptionemployee_job-id-brink_id <----- This is third party id I am saving of relationship for third party-brink_employee_id-brink_job_idIn Employee Model I have created relationshippublic function jobs(){ return $this->belongsToMany('App...
 laravel                     1 answers                     53 view
 2018-10-22         Spring

Responses JSON with Relationship - Laravel/Eloquent

I'm working on a Laravel project and I want to create a REST API for a website. On my system, I have two tables:Blogs and Categories. The table blogs have the category_id column, which is a key that references the column ID in the category table.Blogs Migrationclass CreateBlogsTable extends Migration{ public function up() { Schema::create('blogs', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->longtext('body'); $table->string('category_id'); $table->timestamps(); }); } .....}Catego...
 laravel                     3 answers                     55 view