2017-04-10 291 views
0

这是我的JSON:嵌套对象以JSON

Json

我需要访问extra_services并获得service_name

我知道我可以直接用做GSON但​​问题是,我需要使用getter和setter方法,因为我使用的内部回收的适配器,我该怎么办呢?

这里是我的适配器类,我需要得到的服务名称

public class ExtraServicesAdapter extends RecyclerView.Adapter<ExtraServicesAdapter.ViewHolder> implements View.OnClickListener 
{ 
    private ArrayList<Business> businessList; 
    private Activity activity; 
    private int layoutMolde,idb; 

    public ExtraServicesAdapter(Activity activity, ArrayList<Business> list, int layout) 
    { 
     this.activity = activity; 
     this.businessList = list; 
     layoutMolde = layout; 
    } 

    @Override 
    public ExtraServicesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_services_basic, parent, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) 
    { 
     if(businessList.get(position).getExtra_services()==null) 
     { 
      holder.txtNameServiceBasic.setText("There's nothing to show"); 
     } 
     holder.txtNameServiceBasic.setText(businessList.get(position).getExtra_services()); 
    } 

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

    @Override 
    public void onClick(View v) 
    { 

    } 

    public class ViewHolder extends RecyclerView.ViewHolder 
    { 

     public TextView txtNameServiceBasic; 

     public ViewHolder(View itemView) 
     { 
      super(itemView); 
      txtNameServiceBasic = (TextView) itemView.findViewById(R.id.txtNameServiceBasic); 
     } 
    } 
} 

,这是我的课在哪里getter和setter,我使用

public class Business { 

    private Integer id,rating; 
    private String name, description, cover_url_string, logo_url_string, icon_default,business_name,cover_default,extra_services; 
    private Boolean status; 

    public Business(){} 

    public Business(Integer id,Integer rating,String business_name, String name, String description, String logo_url_string, String cover_default, String icon_default,String cover_url_string,String extra_services) { 

     this.id = id; 
     this.name = name; 
     this.business_name=business_name; 
     this.description = description; 
     this.logo_url_string = logo_url_string; 
     this.cover_url_string = cover_url_string; 
     this.rating=rating; 
     this.icon_default=icon_default; 
     this.cover_default=cover_default; 
     this.extra_services=extra_services; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public Integer getRating() { 
     return rating; 
    } 

    public String getBusiness_name() { 
     return business_name; 
    } 

    public void setBusiness_name(String business_name) { 
     this.business_name = business_name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getLogo_url_string() { 
     return logo_url_string; 
    } 

    public void setLogo_url_string(String logo_url_string) { 
     this.logo_url_string = logo_url_string; 
    } 

    public String getIcon_default() { 

     return icon_default; 
    } 

    public String getCover_default() { 
     return cover_default; 
    } 

    public String getCover_url_string() { 
     return cover_url_string; 
    } 

    public String getExtra_services() { 
     return extra_services; 
    } 

    public void setExtra_services() { 
     this.extra_services=extra_services; 
    } 
} 
+0

extra_services是一个数组,为什么你声明它为一个字符串? – njzk2

+0

您可以创建ExtraService类,并在经营业务类中声明它作为列表中进行getter和setter方法这两个类 –

回答

1

您可以添加一个ExtraServices类包含一个列表ExtraService

ExtraService

public class ExtraService { 
private String Id; 
private String ServiceName; 

public String getId() { 
    return Id; 
} 

public void setId(String Id) { 
    this.Id = Id; 
} 

public String getServiceName() { 
    return ServiceName; 
} 

public void setServiceName(String ServiceName) { 
    this.ServiceName = ServiceName; 
} 

ExtraServices

public class ExtraServices { 
private List<ExtraService> extraServicesList; 

public List<ExtraService> getExtraServicesList() { 
    return extraServicesList; 
} 

public void setExtraServicesList(List<ExtraService> extraServicesList) { 
    this.extraServicesList = extraServicesList; 
}  

public void add(ExtraService extraService){ 
    if(extraServicesList == null){ 
     extraServicesList = new ArrayList<>(); 
    } 
    extraServicesList.add(extraService); 
} 

而在你Business类添加的ExtraServices

private ExtraServices extraServices; 

public ExtraServices getExtraServices() { 
    return extraServices; 
} 

public void setExtraServices (ExtraServices extraServices) { 
    this.extraServices = extraServices; 
} 

getter和setter方法后,你必须做二传手过程,并在你的适配器,你应该做的像这样:

holder.txtNameServiceBasic.setText(businessList.get(position).getExtraServices().getExtraServicesList().get(posistion).getServiceName()); 
+0

对不起,我已经采取了这么长时间回复,而且非常感谢你,这个工作对我来说:) –