Created
October 30, 2017 09:10
-
-
Save SalaSuresh/9dc69e73798107d2387bbd902b3cb7d5 to your computer and use it in GitHub Desktop.
Android RecyclerView Example with Retrofit library using Kotlin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?) { | |
} | |
}) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) | |
} | |
} | |
} |
Thanks bro
thank you very much
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thankyou