Retrofit Library + RecyclerView

Dependencies

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'


RetrofirActivity.java

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginHorizontal="20dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />


RetrofitApi.java

public interface RetrofitApi {

@GET("demo.json")
Call<List<UserModel>> userlist();

}


RetrofitLibActivity.java

public class RetrofitLibActivity extends AppCompatActivity {

RecyclerView recyclerView;
List<UserModel> userlist;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit_lib);

recyclerView = findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

Retrofit retrofit = new Retrofit.Builder().baseUrl("https://nilsn1.github.io/nilscreation/")
.addConverterFactory(GsonConverterFactory.create())
.build();

RetrofitApi retrofitApi = retrofit.create(RetrofitApi.class);

Call<List<UserModel>> call = retrofitApi.userlist();

call.enqueue(new Callback<List<UserModel>>() {
@Override
public void onResponse(Call<List<UserModel>> call, Response<List<UserModel>> response) {

userlist = response.body();
UserAdapter adapter = new UserAdapter(RetrofitLibActivity.this, userlist);
recyclerView.setAdapter(adapter);

}

@Override
public void onFailure(Call<List<UserModel>> call, Throwable t) {
Toast.makeText(RetrofitLibActivity.this, "" + t.getMessage(), Toast.LENGTH_SHORT).show();

}
});

}
}


UserAdapter.java

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

Context context;
List<UserModel> userlist;

public UserAdapter(Context context, List<UserModel> userlist) {
this.context = context;
this.userlist = userlist;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(context).inflate(R.layout.user_item, parent, false);
return new ViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
UserModel user = userlist.get(position);
holder.userId.setText("" + user.getUserid());
holder.userName.setText(user.getUsername());
holder.country.setText(user.getCountry());

}

@Override
public int getItemCount() {
return userlist.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

TextView userId, userName, country;

public ViewHolder(@NonNull View itemView) {
super(itemView);

userId = itemView.findViewById(R.id.userId);
userName = itemView.findViewById(R.id.userName);
country = itemView.findViewById(R.id.country);
}
}
}


user_item.xml

<?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:layout_margin="10dp"
android:background="@drawable/custominput"
android:orientation="vertical">

<TextView
android:id="@+id/userId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/nunito_bold"
android:text="Userid"
android:textColor="@color/text_color"
android:textSize="20sp" />

<TextView
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="@font/nunito_bold"
android:text="Username"
android:textColor="@color/text_color"
android:textSize="20sp" />

<TextView
android:id="@+id/country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="@font/nunito_bold"
android:text="Country"
android:textColor="@color/text_color"
android:textSize="20sp" />

</LinearLayout>


UserModel.java

public class UserModel {

int userid;
String username;
String country;

public UserModel(int userid, String username, String country) {
this.userid = userid;
this.username = username;
this.country = country;
}

public int getUserid() {
return userid;
}

public void setUserid(int userid) {
this.userid = userid;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}
}


Post a Comment

Previous Post Next Post