Created
October 26, 2017 09:04
-
-
Save SalaSuresh/591ec84c2e2afbee77874c52fdc7c7e6 to your computer and use it in GitHub Desktop.
Android RecyclerView Example with Retrofit library.
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="vertical" | |
android:padding="16dp"> | |
<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="Address" /> | |
</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 suresh.androidtest; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
/** | |
* Created by Dumadu on 26-Oct-17. | |
*/ | |
public class ApiClient { | |
private static final String BASE_URL = "https://next.json-generator.com/api/json/get/"; | |
public static Retrofit retrofit = null; | |
public static Retrofit getApiClient() { | |
if (retrofit == null) { | |
retrofit = new 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 suresh.androidtest; | |
import java.util.List; | |
import retrofit2.Call; | |
import retrofit2.http.POST; | |
/** | |
* Created by Dumadu on 26-Oct-17. | |
*/ | |
public interface ApiInterface { | |
@POST("E1Pn7khWG") | |
Call<List<Hospitals>> getHospitalsList(); | |
} |
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 suresh.androidtest; | |
import com.google.gson.annotations.SerializedName; | |
/** | |
* Created by Dumadu on 26-Oct-17. | |
*/ | |
public class Hospitals { | |
public String id; | |
@SerializedName("hospital_name") | |
public String hospitalName; | |
public String address; | |
} |
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"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyler_hospitals" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</RelativeLayout> |
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 suresh.androidtest; | |
import android.content.DialogInterface; | |
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 java.util.List; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.hospitalClickListener { | |
ApiInterface apiInterface; | |
RecyclerView mRecyclerView; | |
List<Hospitals> hospitalses; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mRecyclerView = (RecyclerView) findViewById(R.id.recyler_hospitals); | |
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); | |
apiInterface = ApiClient.getApiClient().create(ApiInterface.class); | |
apiInterface.getHospitalsList().enqueue(new Callback<List<Hospitals>>() { | |
@Override | |
public void onResponse(Call<List<Hospitals>> call, Response<List<Hospitals>> response) { | |
hospitalses = response.body(); | |
mRecyclerView.setAdapter(new RecyclerViewAdapter(hospitalses, MainActivity.this)); | |
} | |
@Override | |
public void onFailure(Call<List<Hospitals>> call, Throwable t) { | |
} | |
}); | |
} | |
@Override | |
public void getItem(int position) { | |
AlertDialog.Builder dialog = new AlertDialog.Builder(this) | |
.setTitle(hospitalses.get(position).hospitalName) | |
.setMessage(hospitalses.get(position).address) | |
.setCancelable(false) | |
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.cancel(); | |
} | |
}); | |
dialog.show(); | |
} | |
} |
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 suresh.androidtest; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.util.List; | |
/** | |
* Created by Dumadu on 26-Oct-17. | |
*/ | |
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> { | |
List<Hospitals> hospitalses; | |
hospitalClickListener hospitalClickListener; | |
public RecyclerViewAdapter(List<Hospitals> hospitalses, hospitalClickListener hospitalClickListener) { | |
this.hospitalses = hospitalses; | |
this.hospitalClickListener = hospitalClickListener; | |
} | |
interface hospitalClickListener { | |
void getItem(int position); | |
} | |
@Override | |
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false); | |
return new RecyclerViewHolder(view, hospitalClickListener); | |
} | |
@Override | |
public void onBindViewHolder(RecyclerViewHolder holder, int position) { | |
holder.textViewName.setText(hospitalses.get(position).id + ". " + hospitalses.get(position).hospitalName); | |
holder.textViewAddress.setText(hospitalses.get(position).address); | |
} | |
@Override | |
public int getItemCount() { | |
return hospitalses.size(); | |
} | |
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { | |
TextView textViewName, textViewAddress; | |
hospitalClickListener hospitalClickListener; | |
public RecyclerViewHolder(View itemView, hospitalClickListener hospitalClickListener) { | |
super(itemView); | |
textViewName = itemView.findViewById(R.id.text_name); | |
textViewAddress = itemView.findViewById(R.id.text_address); | |
this.hospitalClickListener = hospitalClickListener; | |
itemView.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
hospitalClickListener.getItem(getAdapterPosition()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment