Skip to content

Instantly share code, notes, and snippets.

@anandrex5
Forked from ankitchauhan20/MallAdapter.java
Created June 16, 2022 08:48
Show Gist options
  • Save anandrex5/548041de32b9125b8924484987d50024 to your computer and use it in GitHub Desktop.
Save anandrex5/548041de32b9125b8924484987d50024 to your computer and use it in GitHub Desktop.
Path Retrofit
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MallRecyclerView">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mallRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin = "5dp"
android:background="@drawable/cardview_bg"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:id="@+id/itemsCardView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="2dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/item_image"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_gravity="center_vertical"
android:src="@drawable/burger"
android:layout_margin="10dp"
/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="15sp"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/item_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="address"
android:textSize="12sp"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:textColor="@color/black"
android:layout_gravity="center"
/>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.oopstutorial.Adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.oopstutorial.ModelClass.StoreModel;
import com.example.oopstutorial.R;
import java.util.List;
public class MallAdapter extends RecyclerView.Adapter<MallAdapter.ViewHolder> {
List<StoreModel> list;
Context context;
public MallAdapter(List<StoreModel> mList, Context mContext) {
this.list = mList;
this.context = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mall_data_items, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StoreModel storeModel = list.get(position);
holder.itemName.setText(list.get(position).getStoreName());
holder.itemAddress.setText(list.get(position).getStoreAddress());
Glide.with(context)
.load(storeModel.getStoreImageUrl())
.into(holder.itemImage);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView itemImage;
TextView itemName;
TextView itemAddress;
public ViewHolder(@NonNull View itemView) {
super(itemView);
itemImage = itemView.findViewById(R.id.item_image);
itemName = itemView.findViewById(R.id.item_name);
itemAddress = itemView.findViewById(R.id.item_address);
}
}
}
package com.example.oopstutorial.ModelClass;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class MallBodyModel {
@SerializedName("stores")
@Expose
private String stores;
public MallBodyModel() {
}
public MallBodyModel(String stores) {
super();
this.stores = stores;
}
public String getStores() {
return stores;
}
public void setStores(String stores) {
this.stores = stores;
}
}
package com.example.oopstutorial.ModelClass;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class MallModel {
@SerializedName("stores")
@Expose
private List<StoreModel> stores = null;
public MallModel() {
}
public MallModel(List<StoreModel> stores) {
super();
this.stores = stores;
}
public List<StoreModel> getStores() {
return stores;
}
public void setStores(List<StoreModel> stores) {
this.stores = stores;
}
}
package com.example.oopstutorial;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.os.Bundle;
import android.util.Log;
import com.example.oopstutorial.Adapters.MallAdapter;
import com.example.oopstutorial.ModelClass.MallBodyModel;
import com.example.oopstutorial.ModelClass.MallModel;
import com.example.oopstutorial.ModelClass.StoreModel;
import com.example.oopstutorial.databinding.ActivityMallRecyclerViewBinding;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MallRecyclerView extends AppCompatActivity {
ActivityMallRecyclerViewBinding binding;
List<StoreModel> sList = new ArrayList<>();
MallAdapter mallAdapter;
MallBodyModel model = new MallBodyModel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_mall_recycler_view);
apiResponse();
}
private void apiResponse() {
MallRetrofitInstance.StoreData service = MallRetrofitInstance.retrofitInstance().create(MallRetrofitInstance.StoreData.class);
model.setStores("All");
Call<MallModel> call = service.getData(model,"117012361");
call.enqueue(new Callback<MallModel>() {
@Override
public void onResponse(Call<MallModel> call, Response<MallModel> response) {
Log.e("s", "success"+response.body());
sList = (List<StoreModel>) response.body().getStores();
mallAdapter = new MallAdapter(sList,MallRecyclerView.this);
binding.mallRecyclerView.setAdapter(mallAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(MallRecyclerView.this, LinearLayoutManager.VERTICAL, false);
binding.mallRecyclerView.setLayoutManager(layoutManager);
}
@Override
public void onFailure(Call<MallModel> call, Throwable t) {
Log.e("f", "Failed"+t.getMessage());
}
});
}
}
package com.example.oopstutorial;
import com.example.oopstutorial.ModelClass.MallBodyModel;
import com.example.oopstutorial.ModelClass.MallModel;
import com.example.oopstutorial.ModelClass.StoreModel;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.Path;
public class MallRetrofitInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "https://rpcauat-api.reciproci.com";
public interface StoreData{
@POST("/cans/tt/rest/api/sec/v4/MallStores.json/{PATH}")
Call<MallModel> getData(@Body MallBodyModel bodyModel, @Path("PATH") String storeId);
}
public static Retrofit retrofitInstance() {
if (retrofit == null) {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(chain -> {
Request request = chain.request()
.newBuilder()
.addHeader("AUTH_TOKEN", "NJ7FkM0RYiRB4iKR5z4HyQ==")
.addHeader("Content-Type", "application/json")
.addHeader("DEVICE_ID", "edSL638FRMyck1IwMGZXVb")
.addHeader("ANDROID_DEVICE_ID", "5b39185c0342d9f9")
.addHeader("COUNTRY", "India")
.addHeader("CITY", "Belagavi")
.addHeader("LANGUAGE", "EN")
.addHeader("APP_VERSION", "0.1.71")
.addHeader("IS_FILTERED","FALSE")
.addHeader("accept","application/json")
.build();
return chain.proceed(request);
})
.build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
package com.example.oopstutorial.ModelClass;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class StoreModel {
@SerializedName("countryId")
@Expose
private Integer countryId;
@SerializedName("countryName")
@Expose
private String countryName;
@SerializedName("cityId")
@Expose
private Integer cityId;
@SerializedName("cityName")
@Expose
private String cityName;
@SerializedName("locataion")
@Expose
private String locataion;
@SerializedName("mallId")
@Expose
private Integer mallId;
@SerializedName("mallName")
@Expose
private String mallName;
@SerializedName("storeId")
@Expose
private Integer storeId;
@SerializedName("brandId")
@Expose
private Integer brandId;
@SerializedName("brandName")
@Expose
private String brandName;
@SerializedName("brandCode")
@Expose
private String brandCode;
@SerializedName("storeName")
@Expose
private String storeName;
@SerializedName("storeAddress")
@Expose
private String storeAddress;
@SerializedName("storeImageUrl")
@Expose
private String storeImageUrl;
@SerializedName("contactPerson")
@Expose
private String contactPerson;
@SerializedName("contactNumber")
@Expose
private String contactNumber;
@SerializedName("videoUrl")
@Expose
private String videoUrl;
@SerializedName("brandIconUrl")
@Expose
private String brandIconUrl;
@SerializedName("storeWall")
@Expose
private Boolean storeWall;
@SerializedName("lifeStyleProducts")
@Expose
private Boolean lifeStyleProducts;
@SerializedName("lifeStyleProductsImagePath")
@Expose
private String lifeStyleProductsImagePath;
@SerializedName("storeImages")
@Expose
private List<String> storeImages = null;
@SerializedName("mrBrandId")
@Expose
private Integer mrBrandId;
/**
* No args constructor for use in serialization
*
*/
public StoreModel() {
}
/**
*
* @param mallName
* @param brandName
* @param storeImageUrl
* @param lifeStyleProductsImagePath
* @param mallId
* @param lifeStyleProducts
* @param contactPerson
* @param mrBrandId
* @param cityId
* @param storeId
* @param countryId
* @param storeWall
* @param storeImages
* @param cityName
* @param videoUrl
* @param storeAddress
* @param brandId
* @param contactNumber
* @param locataion
* @param storeName
* @param countryName
* @param brandCode
* @param brandIconUrl
*/
public StoreModel(Integer countryId, String countryName, Integer cityId, String cityName, String locataion, Integer mallId, String mallName, Integer storeId, Integer brandId, String brandName, String brandCode, String storeName, String storeAddress, String storeImageUrl, String contactPerson, String contactNumber, String videoUrl, String brandIconUrl, Boolean storeWall, Boolean lifeStyleProducts, String lifeStyleProductsImagePath, List<String> storeImages, Integer mrBrandId) {
super();
this.countryId = countryId;
this.countryName = countryName;
this.cityId = cityId;
this.cityName = cityName;
this.locataion = locataion;
this.mallId = mallId;
this.mallName = mallName;
this.storeId = storeId;
this.brandId = brandId;
this.brandName = brandName;
this.brandCode = brandCode;
this.storeName = storeName;
this.storeAddress = storeAddress;
this.storeImageUrl = storeImageUrl;
this.contactPerson = contactPerson;
this.contactNumber = contactNumber;
this.videoUrl = videoUrl;
this.brandIconUrl = brandIconUrl;
this.storeWall = storeWall;
this.lifeStyleProducts = lifeStyleProducts;
this.lifeStyleProductsImagePath = lifeStyleProductsImagePath;
this.storeImages = storeImages;
this.mrBrandId = mrBrandId;
}
public Integer getCountryId() {
return countryId;
}
public void setCountryId(Integer countryId) {
this.countryId = countryId;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public Integer getCityId() {
return cityId;
}
public void setCityId(Integer cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getLocataion() {
return locataion;
}
public void setLocataion(String locataion) {
this.locataion = locataion;
}
public Integer getMallId() {
return mallId;
}
public void setMallId(Integer mallId) {
this.mallId = mallId;
}
public String getMallName() {
return mallName;
}
public void setMallName(String mallName) {
this.mallName = mallName;
}
public Integer getStoreId() {
return storeId;
}
public void setStoreId(Integer storeId) {
this.storeId = storeId;
}
public Integer getBrandId() {
return brandId;
}
public void setBrandId(Integer brandId) {
this.brandId = brandId;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getBrandCode() {
return brandCode;
}
public void setBrandCode(String brandCode) {
this.brandCode = brandCode;
}
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public String getStoreAddress() {
return storeAddress;
}
public void setStoreAddress(String storeAddress) {
this.storeAddress = storeAddress;
}
public String getStoreImageUrl() {
return storeImageUrl;
}
public void setStoreImageUrl(String storeImageUrl) {
this.storeImageUrl = storeImageUrl;
}
public String getContactPerson() {
return contactPerson;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
public String getBrandIconUrl() {
return brandIconUrl;
}
public void setBrandIconUrl(String brandIconUrl) {
this.brandIconUrl = brandIconUrl;
}
public Boolean getStoreWall() {
return storeWall;
}
public void setStoreWall(Boolean storeWall) {
this.storeWall = storeWall;
}
public Boolean getLifeStyleProducts() {
return lifeStyleProducts;
}
public void setLifeStyleProducts(Boolean lifeStyleProducts) {
this.lifeStyleProducts = lifeStyleProducts;
}
public String getLifeStyleProductsImagePath() {
return lifeStyleProductsImagePath;
}
public void setLifeStyleProductsImagePath(String lifeStyleProductsImagePath) {
this.lifeStyleProductsImagePath = lifeStyleProductsImagePath;
}
public List<String> getStoreImages() {
return storeImages;
}
public void setStoreImages(List<String> storeImages) {
this.storeImages = storeImages;
}
public Integer getMrBrandId() {
return mrBrandId;
}
public void setMrBrandId(Integer mrBrandId) {
this.mrBrandId = mrBrandId;
}
}
@anandrex5
Copy link
Author

// Retrofit Path

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