0
我使用多個Number picker動態出現的回收站視圖。我想要OnValueChange上每個數字選擇器的值。如何使用NumberPicker從Recycler視圖中檢索值
如何實現這一目標?
我的適配器類別
public class MedicationCounterAdapter extends RecyclerView.Adapter<MedicationCounterAdapter.MyViewHolder> {
private List<MedicationCounter> medicationCounterList;
public Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView medicationTitle;
public TextView medicationType;
public NumberPicker numberPickerNP;
public MyViewHolder(View view) {
super(view);
medicationTitle = (TextView) view.findViewById(R.id.med_day_type_tv);
medicationType = (TextView) view.findViewById(R.id.med_type_tv);
numberPickerNP = (NumberPicker) view.findViewById(R.id.number_picker_medication);
}
}
public MedicationCounterAdapter(List<MedicationCounter> medicationCounterList, Context iContext) {
this.medicationCounterList = medicationCounterList;
this.context = iContext;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.include_medication_counter_components, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final MedicationCounter medicationCounter = medicationCounterList.get(position);
holder.medicationTitle.setText(medicationCounter.getMedicationName());
holder.medicationType.setText(medicationCounter.getMedicationDayType());
//Set the minimum value of NumberPicker
holder.numberPickerNP.setMinValue(1);
//Specify the maximum value/number of NumberPicker
holder.numberPickerNP.setMaxValue(10);
//Gets whether the selector wheel wraps when reaching the min/max value.
holder.numberPickerNP.setWrapSelectorWheel(true);
//Set a value change listener for NumberPicker
holder.numberPickerNP.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
Log.e("numberp", "old value" + i + "' " + "new val" + i1 + ", " + numberPicker);
}
});
}
@Override
public int getItemCount() {
return medicationCounterList.size();
}
}
在我片段回收的觀點是
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.medication_details_recycler_view);
mAdapter = new MedicationCounterAdapter(medicationCounterList, getContext());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
您可以在答案中添加更多細節(例如一個小例子可以幫助每個人)? – Adonis