Skip to content

Instantly share code, notes, and snippets.

@SalaSuresh
Created October 30, 2017 09:10
Show Gist options
  • Save SalaSuresh/9dc69e73798107d2387bbd902b3cb7d5 to your computer and use it in GitHub Desktop.
Save SalaSuresh/9dc69e73798107d2387bbd902b3cb7d5 to your computer and use it in GitHub Desktop.
Android RecyclerView Example with Retrofit library using Kotlin.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.suresh.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_demo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
package com.suresh.retrofitdemo
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
/**
* Created by Dumadu on 26-Oct-17.
*/
public class ApiClient {
public var BASE_URL: String = "https://next.json-generator.com/api/json/get/"
public var retrofit: Retrofit? = null
public fun getApiClient(): Retrofit? {
if (retrofit == null) {
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).build()
}
return retrofit
}
}
package com.suresh.retrofitdemo
import retrofit2.Call
import retrofit2.http.POST
/**
* Created by Dumadu on 26-Oct-17.
*/
public interface ApiInterface {
@POST("E1Pn7khWG")
fun getHospitalsList(): Call<ArrayList<Hospitals>>
}
package com.suresh.retrofitdemo
import com.google.gson.annotations.SerializedName
/**
* Created by Dumadu on 26-Oct-17.
*/
public class Hospitals {
public var id: String? = null
@SerializedName("hospital_name")
public var hospitalName: String? = null
public var address: String? = null
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_picture"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="@mipmap/ic_launcher_round" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="@+id/text_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message" />
</LinearLayout>
</LinearLayout>
package com.suresh.retrofitdemo
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.Toast
import com.example.suresh.myapplication.R
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
/**
* Created by Dumadu on 26-Oct-17.
*/
class MainActivity : AppCompatActivity(), RecyclerViewAdapter.hospitalClickListener {
var hospitalsData: ArrayList<Hospitals> = ArrayList()
override fun getItem(position: Int) {
val alertDialog = AlertDialog.Builder(this@MainActivity)
alertDialog.setTitle(hospitalsData.get(position).hospitalName)
alertDialog.setMessage(hospitalsData.get(position).address)
alertDialog.setPositiveButton("OK") { dialog, which ->
Toast.makeText(this@MainActivity, "OK", Toast.LENGTH_SHORT).show()
}
// alertDialog.setNegativeButton("No") { dialog, which ->
// Toast.makeText(this@MainActivity, "No", Toast.LENGTH_SHORT).show()
// }
alertDialog.show()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var recyclerView: RecyclerView = findViewById(R.id.recyclerview_demo)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
var apiInterface: ApiInterface = ApiClient().getApiClient()!!.create(ApiInterface::class.java)
apiInterface.getHospitalsList().enqueue(object : Callback<ArrayList<Hospitals>> {
override fun onResponse(call: Call<ArrayList<Hospitals>>?, response: Response<ArrayList<Hospitals>>?) {
hospitalsData = response?.body()!!
recyclerView.adapter = RecyclerViewAdapter(response?.body()!!, this@MainActivity)
}
override fun onFailure(call: Call<ArrayList<Hospitals>>?, t: Throwable?) {
}
})
}
}
package com.suresh.retrofitdemo
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.example.suresh.myapplication.R
/**
* Created by Dumadu on 26-Oct-17.
*/
class RecyclerViewAdapter(var hospitalsList: ArrayList<Hospitals>?, var itemClick: hospitalClickListener) : RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder>() {
override fun getItemCount(): Int {
return hospitalsList!!.size
}
interface hospitalClickListener {
fun getItem(position: Int)
}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
holder.bindData(hospitalsList, position)
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerViewHolder {
var view: View = LayoutInflater.from(parent!!.context).inflate(R.layout.item_list, parent, false)
return RecyclerViewHolder(view, itemClick)
}
class RecyclerViewHolder(itemView: View, var itemClick: hospitalClickListener) : RecyclerView.ViewHolder(itemView) {
// class RecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var textName: TextView = itemView.findViewById(R.id.text_name)
var textAddress: TextView = itemView.findViewById(R.id.text_address)
fun bindData(hospitalsList: ArrayList<Hospitals>?, position: Int) {
textName.text = hospitalsList!!.get(position).hospitalName
textAddress.text = hospitalsList!!.get(position).address
itemView.setOnClickListener(View.OnClickListener {
itemClick.getItem(adapterPosition)
})
}
}
}
@SuhailAijaz
Copy link

thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment