显示离线时存储在Room数据库中的数据。

huangapple go评论62阅读模式
英文:

Showing data stored in room database when offline

问题

以下是您要翻译的内容:

"I'm fetching data from API I created and storing it in room database. I'm trying to display data stored in the room database when offline but it keeps destroying itself. Please help.

I want if there data in database but not internet to display in recycler view but play to show no network.
If there no internet and no data in database to show not data in recyclerview

Checking whether there is internet

if (!isConnected){
    program.text = "Requires internet"
    //startTime.visibility = View.INVISIBLE
    //endTime.visibility = View.INVISIBLE
    //emptyString.visibility = View.VISIBLE
    //emptyString.text = "No internet"
    apiViewModel.programs.observe(this) {
        programs -> run {
        adapter.submitList(programs)
        // Log.d("Programs", "$programs")
    }
    }
    play.setOnClickListener {
        Toast.makeText(this, "Requires internet", Toast.LENGTH_SHORT).show()
    }
    stop.setOnClickListener {
        finish()
        stopService(Intent(this, BackgroundStreamService::class.java))
    }
} else{
    //startTime.visibility = View.VISIBLE
    //endTime.visibility = View.VISIBLE
    play.setOnClickListener {
        if(isPlaying){
            play.setImageResource(R.drawable.baseline_play_arrow_24)
            stopService(Intent(this, BackgroundStreamService::class.java))
            isPlaying = false
        } else{
            startForegroundService(Intent(this, BackgroundStreamService::class.java))
            isPlaying = true
            //play.setBackgroundResource(R.drawable.baseline_stop_24)
            play.setImageResource(R.drawable.baseline_stop_24)
        }
    }
    stop.setOnClickListener {
        finish()
        stopService(Intent(this, BackgroundStreamService::class.java))
    }
    //fetchPrograms()
    apiViewModel.programs.observe(this) {
        programs -> run {
        adapter.submitList(programs)
        Log.d("Programs", "$programs")
    }
}
}

Repo

val programs: LiveData<List<Program>> = Transformations.map(programDao.getPrograms()){
    it.asDomainModel()
}
suspend fun refreshPrograms(){
    withContext(Dispatchers.IO){
        val programs = NodejsApi.programApi.getProgramAsync().await()
        programDao.insertAll(programs)
    }
}

如果您有其他问题,或需要进一步帮助,请告诉我。

英文:

I'm fetching data from API I created and storing it in room database. I'm trying to display data stored in the room database when offline but it keeps destroying itself. Please help.

I want if there data in database but not internet to display in recycler view but play to show no network.
If there no internet and no data in database to show not data in recyclerview

Checking whether there is internet

if (!isConnected){
            program.text = &quot;Requires internet&quot;
            //startTime.visibility = View.INVISIBLE
            //endTime.visibility = View.INVISIBLE
//            emptyString.visibility = View.VISIBLE
//            emptyString.text = &quot;No internet&quot;
            apiViewModel.programs.observe(this) {

                    programs -&gt; run {
                adapter.submitList(programs)
               // Log.d(&quot;Programs&quot;, &quot;$programs&quot;)
            }
            }
            play.setOnClickListener {
                Toast.makeText(this, &quot;Requires internet&quot;, Toast.LENGTH_SHORT).show()
            }
            stop.setOnClickListener {
                finish()
                stopService(Intent(this, BackgroundStreamService::class.java))
            }

        } else{
            //startTime.visibility = View.VISIBLE
            //endTime.visibility = View.VISIBLE
            play.setOnClickListener {

                if(isPlaying){


                    play.setImageResource(R.drawable.baseline_play_arrow_24)
                    stopService(Intent(this, BackgroundStreamService::class.java))
                    isPlaying = false


                }else{
                    startForegroundService(Intent(this, BackgroundStreamService::class.java))
                    isPlaying = true
                    //play.setBackgroundResource(R.drawable.baseline_stop_24)
                    play.setImageResource(R.drawable.baseline_stop_24)
                }


            }

            stop.setOnClickListener {

                finish()
                stopService(Intent(this, BackgroundStreamService::class.java))

            }
            //fetchPrograms()
            apiViewModel.programs.observe(this) {

                    programs -&gt; run {
                adapter.submitList(programs)
                Log.d(&quot;Programs&quot;, &quot;$programs&quot;)
            }
        }

        }

Repo

val programs: LiveData&lt;List&lt;Program&gt;&gt; = Transformations.map(programDao.getPrograms()){
        it.asDomainModel()
    }
    suspend fun refreshPrograms(){
        withContext(Dispatchers.IO){
            val programs = NodejsApi.programApi.getProgramAsync().await()
            programDao.insertAll(programs)
        }
    }

答案1

得分: 0

如果数据库中没有数据,就在检查手机是否连接到互联网后检查是否有数据。

if (programs.isEmpty()) {
    emptyString.visibility = View.VISIBLE
    emptyString.text = "数据库中没有数据。\n请打开互联网。"
} else {
    adapter.submitList(programs)
}
英文:

Realized that I needed also to check whether there is data in database after checking that if the phone is connected to the internet.

if (programs.isEmpty()){
       emptyString.visibility = View.VISIBLE
       emptyString.text = &quot;No data in the database. \nPlease turn on the internet.&quot;
 }else{
       adapter.submitList(programs)

}

huangapple
  • 本文由 发表于 2023年3月7日 02:29:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定