实际上,当收到JSON响应时,我的同事使用 Builder Pattern (实现Parcelable)而不是getter和setter,所以我想实现RecyclerViewAdapter类来创建listView ... 所以任何人都可以帮助我如何使用RecyclerViewAdapter中的Builder模式类来渲染我们的ListView。
注:
更新:
MainActivity :
填充数据后,我使用
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
});
在 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);
}
}
}
在 ServiceProviderItem:
中public class Provider实现了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);
}
}
}