I am using retrofit, recyclerview and coroutines to build a Quotes displaying app,but the app won't run

Viewed 14

I am new to android and wanted to create a quotes displaying app using an api and show content and author in textview, but the app doesn't even run or open.

1.MainActivity.kt

package com.example.latestnews

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.latestnews.databinding.ActivityMainBinding
import retrofit2.HttpException
import java.io.IOException


const val TAG = "Main Activity"

class MainActivity : AppCompatActivity() {
private lateinit var bind: ActivityMainBinding
private lateinit var adapt: Adapter

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    bind = ActivityMainBinding.inflate(layoutInflater)
    setContentView(bind.root)
    setuprecycleview()

    //coroutine
    lifecycleScope.launchWhenCreated {
        bind.progressBar.isVisible = true
        val response = try {
            RetrofitInstance.api.get()
        } catch (_: IOException) {
            Log.e(TAG, "IOException, you might not have internet connection")
            bind.progressBar.isVisible = false
            return@launchWhenCreated
        } catch (_: HttpException) {
            Log.e(TAG, "HttpException, unexpected response ")
            bind.progressBar.isVisible = false
            return@launchWhenCreated
        }
        if (response.isSuccessful && response.body() != null) {
            bind.progressBar.isVisible = false
            adapt.todo = response.body()!!
        } else {
            Log.e(TAG, "Response not successful")
            bind.progressBar.isVisible = false
        }
    }

}

private fun setuprecycleview() {
    bind.main.apply {
        adapt = Adapter()
        adapter = adapt
        layoutManager = LinearLayoutManager(this@MainActivity)
    }
}}

2.activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/main"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

This is my adapter class 3.Adapter.kt

package com.example.latestnews

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.latestnews.databinding.NewsLayoutBinding

class Adapter:RecyclerView.Adapter<Adapter.QViewHolder>() {

    inner class QViewHolder(val binding: NewsLayoutBinding) :RecyclerView.ViewHolder(binding.root)
    lateinit var todo: Data


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QViewHolder {
        return QViewHolder(NewsLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: QViewHolder, position: Int) {
        holder.binding.apply {
            val items = todo.results[position]
            textView.text=items.content
            textView2.text=items.author
        }
    }

    override fun getItemCount(): Int {
    return todo.count
    }
}

4.Quotesapi.kt

package com.example.latestnews

import retrofit2.Response
import retrofit2.http.GET

interface Quotesapi {
    @GET("/quotes")
    suspend fun get(): Response<Data>
}

5.RetrofitInstance.kt

   package com.example.latestnews

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitInstance {

    val api:Quotesapi by lazy {
        Retrofit.Builder().
        baseUrl("https://quotable.io")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(Quotesapi::class.java)//same name as interface
    }

}

6.Data.kt

package com.example.latestnews

data class Data(
    val count: Int,
    val lastItemIndex: Int,
    val page: Int,
    val results: List<Result>,
    val totalCount: Int,
    val totalPages: Int
)

7.Result.kt

package com.example.latestnews

data class Result(
    val _id: String,
    val author: String,
    val authorSlug: String,
    val content: String,
    val dateAdded: String,
    val dateModified: String,
    val length: Int,
    val tags: List<String>
)

8.news_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="80dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        tools:text="PlaceHolder1"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/textView2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        tools:text="PlaceHolder2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
0 Answers
Related