0

Actually, when the JSON response is received, my colleague use the Builder Pattern(implements Parcelable) instead of getter and setter so I want to implement the RecyclerViewAdapter class for creating a listView... so can anyone help me how can I use the Builder pattern class in RecyclerViewAdapter to render our ListView.

NOTE:

  1. I know how can I do it if he using the getter and setter but I don't know the concept of Builder pattern
  2. and I also know that the Parcelable is used for passing the object from one activity to the another activity but I never used it before so please help me

UPDATED:

In MainActivity:

After populating the data i use

ArrayList<ServiceProviderItem> workshopList;
mAdapter=new ServiceProviderAdapter(this,workshopList);
        mRecyclerView.setAdapter(mAdapter);

//volley on response, i used the below code for populating...If i wrong please correct me
 JSONObject item = workshops.getJSONObject(i);
                                ServiceProviderItem serviceProvider = new ServiceProviderItem(new ServiceProviderItem.Builder(
                                        Const.TAG_WORKSHOP_ID, Const.TAG_CITY, Const.TAG_WORKSHOP_NAME, Const.TAG_LAT, Const.TAG_LON, Const.TAG_LOGO, Const.TAG_ID_CAR).optional(
                                        Const.TAG_LOCALITIES,
                                        Const.TAG_ADDRESS
    });

In ServiceProviderAdapter:

public class ServiceProviderAdapter extends RecyclerView.Adapter<ServiceProviderAdapter.ViewHolder>{
ArrayList<ProviderItem> mItemsList;
Context context;


public ServiceProviderAdapter(ArrayList<ProviderItem> mItemsList,Context context){
    super();
        this.mItemsList = mItemsList;
        this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_find,parent, false);

    ViewHolder viewHolder = new ViewHolder(v);

    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ProviderItem providerItem= mItemsList.get(position);

   //  holder.ServiceCentreName.setText();
//how to get the Data from the Builder Pattern class... again i know how to use getter setter but i don't know how to retreive from the Builder class

}

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

class ViewHolder extends RecyclerView.ViewHolder{
    public TextView ServiceCentreName,ServiceCentreAddress,CentreDistance,ServicePrice;
    public RatingBar ratingBar;
    public ViewHolder(View itemView) {

        super(itemView);
        ServiceCentreName=(TextView)itemView.findViewById(R.id.ServiceproviderName);
        ServiceCentreAddress=(TextView)itemView.findViewById(R.id.ServiceproviderAddress);
        CentreDistance=(TextView)itemView.findViewById(R.id.ServiceproviderDistance);
        ServicePrice=(TextView)itemView.findViewById(R.id.service_price);
        ratingBar=(RatingBar)itemView.findViewById(R.id.ServiceProviderRating);


    }
}
}

In ServiceProviderItem:

public class Provider implements Parcelable{

String workshop_id, city, workshop_name, localities, address, phone,  email, lat, lon, working_day, timings, active_flag,  logo, service, repair,
carwash, discount,  id_car;

public Provider(Builder builder)
{
    this.workshop_id = builder.workshop_id;
    this.city = builder.city;
    this.workshop_name = builder.workshop_name;
    this.localities = builder.workshop_name;
    ....
}

protected Provider(Parcel in)
{
    this.workshop_id = in.readString();
    this.city = in.readString();
    this.workshop_name = in.readString();
    this.localities = in.readString();
   ......
}


@Override
public int describeContents() {
    return hashCode();
}

@Override
public void writeToParcel(Parcel dest, int flags)
{
    dest.writeString(workshop_id);
    dest.writeString(city);
    dest.writeString(workshop_name);
    dest.writeString(localities);
    ....
}

public static final Creator<Provider> CREATOR = new Creator<Provider>() {
    @Override
    public Provider createFromParcel(Parcel in) {
        return new Provider(in);
    }

    @Override
    public Provider[] newArray(int size) {
        return new Provider[size];

    }
};


public static class Builder
{
    String workshop_id, city, workshop_name, localities, address, phone,  email, lat, lon, working_day, timings, active_flag,  logo, service, repair,
            carwash, discount,  id_car;

    public Builder(String workshop_id, String city, String workshop_name, String lat, String lon, String logo, String id_car)
    {
        this.workshop_id = workshop_id;
        this.city = city;
        this.workshop_name = workshop_name;
        this.lat = lat;
        this.lon = lon;
        this.logo = logo;
        this.id_car = id_car;
        ....
    }

    public Builder localities(String localities) {
        this.localities = localities;
        return  this;
    }

    public Builder address(String address) {
        this.address = address;
        return this;
    }

    public Builder phone(String phone) {
        this.phone = phone;
        return this;
    }

    public Builder email(String email) {
        this.email = email;
        return this;
    }

    public Builder working_day(String working_day) {
        this.working_day = working_day;
        return this;
    }

    public Builder timings(String timings) {
        this.timings = timings;
        return this;
    }

    public Builder active_flag(String active_flag) {
        this.active_flag = active_flag;
        return this;
    }

    public Builder service(String service) {
        this.service = service;
        return this;
    }

    public Builder repair(String repair) {
        this.repair = repair;
        return this;
    }

    public Builder carwash(String carwash) {
        this.carwash = carwash;
        return this;
    }

    public Builder discount(String discount) {
        this.discount = discount;
        return this;
    }

    public Builder optional(String localities, String address, String phone, String email, String working_day, String timings, String active_flag,
                                        String service, String repair, String carwash, String discount) {
        this.localities = localities;
        this.address = address;
        this.phone = phone;
        this.email = email;
        this.working_day = working_day;
        this.timings = timings;
        this.active_flag = active_flag;
        this.service = service;
        this.repair = repair;
        this.carwash = carwash;
        this.discount = discount;

        return this;
    }

    public Provider  Build()
    {
        return new Provider(this);
    }

}

}
Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
  • i know that.. and i read out many answers but none is fit with my problem.. so can u guide me the how can i use the Builder class in RecyclerViewAdapter or any link please –  Jan 30 '17 at 10:38
  • 1
    don't so be rude.. i m just asking for the help as i m new in android development –  Jan 30 '17 at 10:44
  • for parcelable refer this answer http://stackoverflow.com/a/28871200/1576416 and for builder pattern refer this https://sourcemaking.com/design_patterns/builder/java/2 – Amrut Bidri Jan 31 '17 at 09:00
  • want to know parceable with builder pattern –  Jan 31 '17 at 09:05

0 Answers0