implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation("androidx.recyclerview:recyclerview:1.2.1")
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
APIService
interface APIService {
@GET("?results=5")
fun fetchUser(): Call<UserResponseNew>
}
RestClient
object RestClient {
private val BASE_URL = "https://randomuser.me/api/"
private var mRetrofit: Retrofit? = null
val client: Retrofit
get() {
if (mRetrofit == null) {
mRetrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return this.mRetrofit!!
}
}
ExampleAdapter
class ExampleAdapter(private val userList: List<ResultsItem>) :
RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.item_row, parent,
false
)
return ExampleViewHolder(itemView)
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = userList[position]
holder.textView.text = currentItem.email
Log.d("TAG", "DATA:: " + currentItem.location?.street?.name)
//holder.textView.text = currentItem.results?.get(0)?.email
Glide.with(holder.itemView.context).load(currentItem.picture?.thumbnail).into(holder.imageView);
holder.tvCity.text = currentItem.location?.street?.name
}
override fun getItemCount() = userList.size
class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView: TextView = itemView.findViewById(R.id.tv_item_name)
val imageView: ImageView = itemView.findViewById(R.id.pic)
val tvCity: TextView = itemView.findViewById(R.id.tv_item_city)
}
}
MainActivity
class MainActivity : AppCompatActivity() {
private val TAG = MainActivity::class.java.simpleName
private lateinit var recyclerView: RecyclerView
private var mApiService: APIService? = null
//private var userResponse = UserResponseNew()
private var mUserList: MutableList<ResultsItem> = ArrayList()
//private var categoryList: List<ResultsItem>? = null
//var list = ArrayList<ResultsItem>()
var list: List<ResultsItem> = listOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
mApiService = RestClient.client.create(APIService::class.java)
userListAPI()
}
private fun userListAPI() {
val call = mApiService!!.fetchUser()
call.enqueue(object : retrofit2.Callback<UserResponseNew?> {
override fun onResponse(
call: Call<UserResponseNew?>, response: Response<UserResponseNew?>) {
val userResponse = response.body()!!
Log.d(TAG, "data_size:: " + userResponse.results?.size)
mUserList.addAll(userResponse.results!!)
//RecyclerView
recyclerView.layoutManager = LinearLayoutManager(
this@MainActivity,
RecyclerView.VERTICAL, false
)
val exampleAdapter = ExampleAdapter(mUserList)
recyclerView.adapter = exampleAdapter
val responseBody = response.body()!!
//Toast
Toast.makeText(this@MainActivity, "Success", Toast.LENGTH_SHORT).show()
//Single record print
Log.d(TAG, "email_1:: " + responseBody.results?.get(0)?.email)
//Log.d(TAG, "title_1:: "+responseBody.results?.get(0)?.name?.title)
//All record print using for loop
for (myData in responseBody.results!!) {
//if (myData != null) {
Log.d(TAG, "email:: " + myData.email)
//}
}
}
override fun onFailure(call: Call<UserResponseNew?>, t: Throwable) {
//Toast
Toast.makeText(this@MainActivity, "Fail", Toast.LENGTH_SHORT).show()
}
})
}
}