简体   繁体   English

如何使用NumberPicker从Recycler View中检索值

[英]How to retrieve value from Recycler View with NumberPicker

I am having recycler view with dynamic occurrence of multiple Number picker. 我有动态出现多个号码选择器的回收站视图。 I want the value of each number picker on OnValueChange. 我想要OnValueChange上每个数字选择器的值。

How to achieve this ? 如何实现呢?

My Adapter Class 我的适配器类

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();
}

}

and recycler view in my fragment is 我片段中的回收站视图是

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);

在MedicationCounter类中定义一个int成员,并在onValueChange方法中在特定位置为其分配i1值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM