2016-08-15 40 views
1

我剛剛跳到Android與一個非常有限的有關recyclerview的知識。我已經使用AA位,但面臨的情況是與me.Check設置onBindViewHolder.Please熊市裏面的文字如下代碼:RecyclerView設置文本

1.本是JSON,在這裏:

[ 
    { 
    "restaurantSubscriptionType_Id": 1, 
    "noOfDaysPlan": "1 Day Trial Meal", 
    "restaurantSubscriptionEntity": [ 
     { 
     "restaurantSubscriptionId": 39, 
     "subscriptionPlan": "Breakfast & Lunch", 
     "subscriptionImagePath": null, 
     "subscriptionDays": 1, 
     "subscriptionAmount": 150, 
     "restaurant_Subscription_Type_Id": 1 
     } 
    ], 
    "restaurantId": 224 
    }, 
    { 
    "restaurantSubscriptionType_Id": 2, 
    "noOfDaysPlan": "5 Days Weekly Plan", 
    "restaurantSubscriptionEntity": [ 
     { 
     "restaurantSubscriptionId": 40, 
     "subscriptionPlan": "Breakfast & Lunch", 
     "subscriptionImagePath": null, 
     "subscriptionDays": 5, 
     "subscriptionAmount": 650, 
     "restaurant_Subscription_Type_Id": 2 
     }, 
     { 
     "restaurantSubscriptionId": 41, 
     "subscriptionPlan": "Only Lunch", 
     "subscriptionImagePath": null, 
     "subscriptionDays": 5, 
     "subscriptionAmount": 500, 
     "restaurant_Subscription_Type_Id": 2 
     } 
    ], 
    "restaurantId": 224 
    } 
] 

我有pojo2schema網站

2.RestaurantSubscriptionEntity

public class RestaurantSubscriptionEntity { 
private Integer restaurantSubscriptionId; 
private String subscriptionPlan; 
private String subscriptionImagePath; 
private Integer subscriptionDays; 
private Long subscriptionAmount; 
private Integer restaurantSubscriptionTypeId; 

public RestaurantSubscriptionEntity(){ 

} 

public Integer getRestaurantSubscriptionId() { 
    return restaurantSubscriptionId; 
} 

public void setRestaurantSubscriptionId(Integer restaurantSubscriptionId) { 
    this.restaurantSubscriptionId = restaurantSubscriptionId; 
} 

public String getSubscriptionPlan() { 
    return subscriptionPlan; 
} 

public void setSubscriptionPlan(String subscriptionPlan) { 
    this.subscriptionPlan = subscriptionPlan; 
} 

public String getSubscriptionImagePath() { 
    return subscriptionImagePath; 
} 

public void setSubscriptionImagePath(String subscriptionImagePath) { 
    this.subscriptionImagePath = subscriptionImagePath; 
} 

public Integer getSubscriptionDays() { 
    return subscriptionDays; 
} 

public void setSubscriptionDays(Integer subscriptionDays) { 
    this.subscriptionDays = subscriptionDays; 
} 

public Long getSubscriptionAmount() { 
    return subscriptionAmount; 
} 

public void setSubscriptionAmount(Long subscriptionAmount) { 
    this.subscriptionAmount = subscriptionAmount; 
} 

public Integer getRestaurantSubscriptionTypeId() { 
    return restaurantSubscriptionTypeId; 
} 

public void setRestaurantSubscriptionTypeId(Integer restaurantSubscriptionTypeId) { 
    this.restaurantSubscriptionTypeId = restaurantSubscriptionTypeId; 
} 

3.DemoItemAll:

public class DemoItemAll { 


private Integer restaurantSubscriptionTypeId; 
private String noOfDaysPlan; 

private List<RestaurantSubscriptionEntity> restaurantSubscriptionEntity = new ArrayList<RestaurantSubscriptionEntity>(); 

private Long restaurantId; 

public DemoItemAll(){ 

} 

public Integer getRestaurantSubscriptionTypeId() { 
    return restaurantSubscriptionTypeId; 
} 

public void setRestaurantSubscriptionTypeId(Integer restaurantSubscriptionTypeId) { 
    this.restaurantSubscriptionTypeId = restaurantSubscriptionTypeId; 
} 

public String getNoOfDaysPlan() { 
    return noOfDaysPlan; 
} 

public void setNoOfDaysPlan(String noOfDaysPlan) { 
    this.noOfDaysPlan = noOfDaysPlan; 
} 

public List<RestaurantSubscriptionEntity> getRestaurantSubscriptionEntity() { 
    return restaurantSubscriptionEntity; 
} 

public void setRestaurantSubscriptionEntity(List<RestaurantSubscriptionEntity> restaurantSubscriptionEntity) { 
    this.restaurantSubscriptionEntity = restaurantSubscriptionEntity; 
} 

public Long getRestaurantId() { 
    return restaurantId; 
} 

public void setRestaurantId(Long restaurantId) { 
    this.restaurantId = restaurantId; 
} 

4.Here我的課延伸RecyclerView

public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.MyViewHolder> { 

    private List<DemoItemAll> demoItemAllArrayList = new ArrayList<>(); 

    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 

    public static class MyViewHolder extends RecyclerView.ViewHolder { 
     private TextView title_tv,price_tv; 
     private Button drop_down_button; 

     public MyViewHolder(View itemView) { 
      super(itemView); 

      title_tv = (TextView) itemView.findViewById(R.id.day_plan_demo_tv_id); 
      price_tv = (TextView) itemView.findViewById(R.id.price_demo_tv_id); 
      drop_down_button = (Button) itemView.findViewById(R.id.demo_drop_down_btn_id); 



     } 
    } 


    public DemoAdapter(List<DemoItemAll> demoItemAllArrayList) { 
     this.demoItemAllArrayList = demoItemAllArrayList; 

    } 

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

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 

     DemoItemAll demoItemAll=new DemoItemAll(); 

     //noOfDays 
     holder.title_tv.setText(demoItemAll.getNoOfDaysPlan()); 

    } 


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

    } 
} 

現在這裏裏面onBindView,我需要設置,但 「subscriptionAmount」 屬於RestaurantSubscriptionEntity class.How做我訪問

holder.price_tv.setText// subscriptionAmount。

+1

這一切首先是*不*了'RecyclerView'問題。這是一個設計問題,我看到你的'Adapter'中有'DemoItemAll'的demoItemAllArrayList,它包含'RestaurantSubscriptionEntity'列表。問題是你想要一個'DemoItemAll','RestaurantSubscriptionEntity'或者他們兩個的列表。請解釋!也重讀你的問題,儘量簡潔通用。 – Abbas

+0

是的,我確實想要一個RestaurantSubscriptionEntity的列表,我想要訪問它的字段 – notTdar

+0

那麼爲什麼你在你的適配器中有'List demoItemAllArrayList'?爲什麼不簡單地將適配器傳遞給'RestaurantSubscriptionEntity'項目的實際列表。 – Abbas

回答

0
@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 

    DemoItemAll demoItemAll = demoItemAllArrayList.get(position); 
    List<RestaurantSubscriptionEntity> entity = demoItemAll.getRestaurantSubscriptionEntity(); 

    holder.title_tv.setText(demoItemAll.getNoOfDaysPlan()); 
    holder.price_tv.setText(String.valueOf(entity.getSubscriptionAmount())) 
} 
0

您只需創建新的空DemoItemAll對象。

您需要使用的

DemoItemAll demoItemAll = demoItemAllArrayList.get(position);

代替

DemoItemAll demoItemAll = new DemoItemAll();