Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从“回收器”视图中的所有数字选择器中获取值?_Android_Class_Android Recyclerview_Options_Numberpicker - Fatal编程技术网

Android 如何从“回收器”视图中的所有数字选择器中获取值?

Android 如何从“回收器”视图中的所有数字选择器中获取值?,android,class,android-recyclerview,options,numberpicker,Android,Class,Android Recyclerview,Options,Numberpicker,大家好,这是我的代码和回收器视图,它可以从我的数据库中收取3个选项的费用,但我的问题是当我试图在我的回收器视图中获取所有3个值时 首先,这是我的课: public class OpcionesPonderacionClass { String Titulo; public OpcionesPonderacionClass(String titulo) { Titulo = titulo; } public OpcionesPonderacionClass() { } publi

大家好,这是我的代码和回收器视图,它可以从我的数据库中收取3个选项的费用,但我的问题是当我试图在我的回收器视图中获取所有3个值时

首先,这是我的课:

public class OpcionesPonderacionClass {
String Titulo;

public OpcionesPonderacionClass(String titulo) {
    Titulo = titulo;
}
 public OpcionesPonderacionClass() {

 }

public String getTitulo() {
    return Titulo;
}

public void setTitulo(String titulo) {
    Titulo = titulo;
}
}

以下是我的适配器代码:

public class OpcionesPonderacionAdapter extends RecyclerView.Adapter<OpcionesPonderacionAdapter.ViewHolderOpcionesPonderacion> {

Context mCtx;
ArrayList<OpcionesPonderacionClass> opcionesPonderacionList;


public OpcionesPonderacionAdapter(Context mCtx, ArrayList<OpcionesPonderacionClass> opcionesList) {
    this.mCtx = mCtx;
    this.opcionesPonderacionList = opcionesList;
}

@NonNull
@Override
public ViewHolderOpcionesPonderacion onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view  = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemopcionponderacion,null);
    return new ViewHolderOpcionesPonderacion(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolderOpcionesPonderacion holder, final int position) {
    holder.tituloOpcionPonderacion.setText(opcionesPonderacionList.get(position).getTitulo());

    holder.npValueOpcionPonderacion.setMinValue(0);
    holder.npValueOpcionPonderacion.setMaxValue(10);

}

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

public class ViewHolderOpcionesPonderacion extends RecyclerView.ViewHolder {
    TextView tituloOpcionPonderacion;
    NumberPicker npValueOpcionPonderacion;

    public ViewHolderOpcionesPonderacion(@NonNull View itemView) {
        super(itemView);
        tituloOpcionPonderacion = itemView.findViewById(R.id.tv_TituloOpcionPonderacion);
        npValueOpcionPonderacion = itemView.findViewById(R.id.np_ValorNumero);

    }
}
公共类OpcionesPounderacionaAdapter扩展了RecyclerView.Adapter{
上下文mCtx;
ArrayList OpcionesPoundacionList;
公共OpcionesPounderacionaAdapter(上下文mCtx、ArrayList opcionesList){
this.mCtx=mCtx;
this.optionesPounderacionList=optionesList;
}
@非空
@凌驾
public viewHolderOpcionesOnCreateViewHolder(@NonNull ViewGroup父级,int viewType){
View-View=LayoutFlater.from(parent.getContext()).inflate(R.layout.ItemOpcionUnderacion,null);
返回新的ViewHolderOpcionesPonderacion(视图);
}
@凌驾
BindViewHolder上的public void(@NonNull ViewHolderOpcionesPonderacion holder,final int位置){
holder.tituLoopcionUnderacion.setText(opcionesPoundacionList.get(position.getTitulo());
holder.npvalueopcionunderacion.setMinValue(0);
holder.npvalueopcionunderacion.setMaxValue(10);
}
@凌驾
public int getItemCount(){
返回OpcionesPounderacionList.size();
}
公共类viewHolderOpcionesOnDeracion扩展了RecyclerView.ViewHolder{
TextView-TituLoopcionUnderacion;
NumberPicker NPValueOpcionUnderacion;
public viewHolderOpcionesOnDeracion(@NonNull View itemView){
超级(项目视图);
tituLoopcionUnderacion=itemView.findViewById(R.id.tv\u tituLoopcionUnderacion);
npvalueopcionpenderacion=itemView.findviewbyd(R.id.np_ValorNumero);
}
}
}

我可以看到从0到10的numberpickers的所有选项,但现在我正在尝试用逗号“,”分隔所有值


我的主要问题是如何从生成的所有数字选择器中获取.getValue()。

您可以设置侦听器以观察NumberPicker中的值更改,并在某个集合中收集值

public static class OpcionesPonderacionAdapter
        extends RecyclerView.Adapter<OpcionesPonderacionAdapter.ViewHolderOpcionesPonderacion> {

    Context mCtx;
    ArrayList<OpcionesPonderacionClass> opcionesPonderacionList;

    private final Map<Integer, Integer> pickerValues = new HashMap<>();
    private OnNumberPickerValueChangeListener valueChangeListener
            = new OnNumberPickerValueChangeListener() {
        @Override
        public void onValueChange(int position, int value) {
            pickerValues.put(position, value);
        }
    };

    public OpcionesPonderacionAdapter(Context mCtx, ArrayList<OpcionesPonderacionClass> opcionesList) {
        this.mCtx = mCtx;
        this.opcionesPonderacionList = opcionesList;
    }

    @NonNull
    @Override
    public ViewHolderOpcionesPonderacion onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemopcionponderacion, null);
        return new ViewHolderOpcionesPonderacion(view, valueChangeListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolderOpcionesPonderacion holder, final int position) {
        holder.tituloOpcionPonderacion.setText(opcionesPonderacionList.get(position).getTitulo());

        holder.npValueOpcionPonderacion.setMinValue(0);
        holder.npValueOpcionPonderacion.setMaxValue(10);

    }

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

    public Map<Integer, Integer> getPickerValues() {
        return pickerValues;
    }

    public class ViewHolderOpcionesPonderacion extends RecyclerView.ViewHolder {

        TextView tituloOpcionPonderacion;
        NumberPicker npValueOpcionPonderacion;

        public ViewHolderOpcionesPonderacion(@NonNull View itemView,
                                             final OnNumberPickerValueChangeListener listener) {
            super(itemView);
            tituloOpcionPonderacion = itemView.findViewById(R.id.tv_TituloOpcionPonderacion);
            npValueOpcionPonderacion = itemView.findViewById(R.id.np_ValorNumero);
            npValueOpcionPonderacion.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker,
                                          int oldVal,
                                          int newVal) {
                    listener.onValueChange(getAdapterPosition(), newVal);
                }
            });
        }
    }

    interface OnNumberPickerValueChangeListener {
        void onValueChange(int position, int value);
    }
}
公共静态类OpcionesPounderacioAdapter
扩展RecyclerView.Adapter{
上下文mCtx;
ArrayList OpcionesPoundacionList;
private final Map pickerValues=new HashMap();
私有OnNumberPickerValueChangeListener值ChangeListener
=新OnNumberPickerValueChangeListener(){
@凌驾
public void onValueChange(整型位置,整型值){
pickerValues.put(位置、值);
}
};
公共OpcionesPounderacionaAdapter(上下文mCtx、ArrayList opcionesList){
this.mCtx=mCtx;
this.optionesPounderacionList=optionesList;
}
@非空
@凌驾
public viewHolderOpcionesOnCreateViewHolder(@NonNull ViewGroup父级,int viewType){
View-View=LayoutInflater.from(parent.getContext()).inflate(R.layout.ItemOpcionUnderacion,null);
返回新的ViewHolderOpcionesPonderacion(视图、值更改侦听器);
}
@凌驾
BindViewHolder上的public void(@NonNull ViewHolderOpcionesPonderacion holder,final int位置){
holder.tituLoopcionUnderacion.setText(opcionesPoundacionList.get(position.getTitulo());
holder.npvalueopcionunderacion.setMinValue(0);
holder.npvalueopcionunderacion.setMaxValue(10);
}
@凌驾
public int getItemCount(){
返回OpcionesPounderacionList.size();
}
公共映射getPickerValues(){
返回pickervalue;
}
公共类viewHolderOpcionesOnDeracion扩展了RecyclerView.ViewHolder{
TextView-TituLoopcionUnderacion;
NumberPicker NPValueOpcionUnderacion;
public viewHolderOpcionesOnDeracion(@NonNull View itemView,
最终OnNumberPickerValueChangeListener(监听器){
超级(项目视图);
tituLoopcionUnderacion=itemView.findViewById(R.id.tv\u tituLoopcionUnderacion);
npvalueopcionpenderacion=itemView.findviewbyd(R.id.np_ValorNumero);
NPValueOpcionUnderacion.setOnValueChangedListener(新的NumberPicker.OnValueChangeListener(){
@凌驾
值更改时的公共无效(编号选择器,
因特奥德瓦尔,
int newVal){
onValueChange(getAdapterPosition(),newVal);
}
});
}
}
NumberPickerValueChangeListener上的接口{
void onValueChange(int位置,int值);
}
}
要获取值,请执行以下操作:

@Override
public void onClick(View v) {
    final Collection<Integer> values = adapter.getPickerValues().values();
    final String result = TextUtils.join(".", values);
    Toast.makeText(this, result ,Toast.LENGTH_SHORT).show();
}
@覆盖
公共void onClick(视图v){
最终集合值=adapter.getPickerValues().values();
最终字符串结果=TextUtils.join(“.”,值);
Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
}

您可以将侦听器设置为观察NumberPicker中的值更改,并在某个集合中收集值

public static class OpcionesPonderacionAdapter
        extends RecyclerView.Adapter<OpcionesPonderacionAdapter.ViewHolderOpcionesPonderacion> {

    Context mCtx;
    ArrayList<OpcionesPonderacionClass> opcionesPonderacionList;

    private final Map<Integer, Integer> pickerValues = new HashMap<>();
    private OnNumberPickerValueChangeListener valueChangeListener
            = new OnNumberPickerValueChangeListener() {
        @Override
        public void onValueChange(int position, int value) {
            pickerValues.put(position, value);
        }
    };

    public OpcionesPonderacionAdapter(Context mCtx, ArrayList<OpcionesPonderacionClass> opcionesList) {
        this.mCtx = mCtx;
        this.opcionesPonderacionList = opcionesList;
    }

    @NonNull
    @Override
    public ViewHolderOpcionesPonderacion onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemopcionponderacion, null);
        return new ViewHolderOpcionesPonderacion(view, valueChangeListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolderOpcionesPonderacion holder, final int position) {
        holder.tituloOpcionPonderacion.setText(opcionesPonderacionList.get(position).getTitulo());

        holder.npValueOpcionPonderacion.setMinValue(0);
        holder.npValueOpcionPonderacion.setMaxValue(10);

    }

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

    public Map<Integer, Integer> getPickerValues() {
        return pickerValues;
    }

    public class ViewHolderOpcionesPonderacion extends RecyclerView.ViewHolder {

        TextView tituloOpcionPonderacion;
        NumberPicker npValueOpcionPonderacion;

        public ViewHolderOpcionesPonderacion(@NonNull View itemView,
                                             final OnNumberPickerValueChangeListener listener) {
            super(itemView);
            tituloOpcionPonderacion = itemView.findViewById(R.id.tv_TituloOpcionPonderacion);
            npValueOpcionPonderacion = itemView.findViewById(R.id.np_ValorNumero);
            npValueOpcionPonderacion.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker,
                                          int oldVal,
                                          int newVal) {
                    listener.onValueChange(getAdapterPosition(), newVal);
                }
            });
        }
    }

    interface OnNumberPickerValueChangeListener {
        void onValueChange(int position, int value);
    }
}
公共静态类OpcionesPounderacioAdapter
扩展RecyclerView.Adapter{
上下文mCtx;
ArrayList OpcionesPoundacionList;
private final Map pickerValues=new HashMap();
私有OnNumberPickerValueChangeListener值ChangeListener
=新OnNumberPickerValueChangeListener(){
@凌驾
public void onValueChange(整型位置,整型值){
pickerValues.put(位置、值);
}
};
公共OpcionesPounderacionaAdapter(上下文mCtx、ArrayList opcionesList){
this.mCtx=mCtx;
this.optionesPounderacionList=optionesList;
}
@非空
@凌驾
public viewHolderOpcionesOnCreateViewHolder(@NonNull ViewGroup父级,int viewType){
View-View=LayoutInflater.from(parent.getContext()).inflate(R.layout.ItemOpcionUnderacion,null);
返回新的ViewHolderOpcionesPonderacion(视图、值更改侦听器);
}
@凌驾
public void onBindViewHolder(@NonNull视图