0

Buenas tardes.

Para ser más especifico cree un formulario dinámico usando reciclerView, este reciclerview puede contener múltiples campos de texto, el problema que pasa es que al volver editar un campo de texto me cambia de posición los valores de los otros campos de texto, aquí les dejo un vídeo del problema en donde se visualiza claramente que el campo denominado Type Camera toma el valor de una fecha, esto sucede al final del vídeo.

https://youtu.be/nI9ztthooRw

Este es mi adaptador

public class rvadapter extends RecyclerView.Adapter<rvadapter.ViewHolder> {

    public Context mContext;
    public ArrayList<FormBody> mPlayers;
    private Calendar mCurrent;


    @Override
    public long getItemId(int position) {
        Object listItem = mPlayers.get(position);
        return listItem.hashCode();
    }

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

    public rvadapter(Context mContext,
                     ArrayList<FormBody> mPlayers) {
        this.mContext = mContext;
        this.mPlayers = mPlayers;

        setHasStableIds(true);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_title;
        private EditText et_value;


        private ViewHolder(View view) {
            super(view);

            tv_title = (TextView) view.findViewById(R.id.tv_title);
            et_value = (EditText) view.findViewById(R.id.et_value);


        }
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.rv_form_body,
                        parent,
                        false);

        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder,
                                 final int pos) {


        final FormBody fb = mPlayers.get(pos);

        switch ( fb.get_type() ){


            //type text
            case 1:

                //Se agrega icono de requirido si corresponse
                prepareTitle(holder, fb);
                fb.setPos_adapter(pos);

                holder.et_value.setId(fb.get_number_col());
                holder.et_value.setInputType(InputType.TYPE_CLASS_TEXT);
                holder.et_value.setHint(R.string.texts);

                holder.et_value.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_keyboard,
                        0,
                        0,
                        0);

                holder.et_value.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    }
                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        fb.setValue( holder.et_value.getText().toString() );
                    }
                    @Override
                    public void afterTextChanged(Editable editable) {
                    }
                });

                break;


            //Type int
            case 2:

                //Se agrega icono de requirido si corresponse
                prepareTitle(holder, fb);
                fb.setPos_adapter(pos);

                holder.et_value.setId(fb.get_number_col());
                holder.et_value.setInputType(InputType.TYPE_CLASS_NUMBER);
                holder.et_value.setHint(R.string.numbers);

                holder.et_value.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_keyboard,
                        0,
                        0,
                        0);

                holder.et_value.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    }
                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        fb.setValue( holder.et_value.getText().toString() );
                    }
                    @Override
                    public void afterTextChanged(Editable editable) {
                    }
                });

                break;



            //Tipe date
            case 3:

                //Se agrega icono de requirido si corresponse
                prepareTitle(holder, fb);
                fb.setPos_adapter(pos);

                holder.et_value.setId(fb.get_number_col());
                holder.et_value.setInputType(InputType.TYPE_CLASS_TEXT);
                holder.et_value.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_calendar,
                        0,
                        0,
                        0);

                holder.et_value.setCursorVisible(false);
                holder.et_value.setFocusable(false);

                holder.et_value.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {


                    mCurrent = Calendar.getInstance();

                    DatePickerDialog dialog = new DatePickerDialog(mContext,
                        android.R.style.Theme_Holo_Light_Dialog,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker datePicker, int year, int month, int day) {

                                mCurrent.set(Calendar.YEAR,  year);
                                mCurrent.set(Calendar.MONTH, month);
                                mCurrent.set(Calendar.DAY_OF_MONTH, day);

                                SimpleDateFormat dateFormat_obj = new SimpleDateFormat("yyyy-MM-dd");
                                SimpleDateFormat dateFormat_et  = new SimpleDateFormat("dd-MM-yyyy");

                                //Set Object
                                fb.setValue( dateFormat_obj.format(mCurrent.getTime()));

                                //Set et
                                holder.et_value.setText(dateFormat_et.format(mCurrent.getTime()));

                            }

                        }, mCurrent.get(Calendar.YEAR),
                        mCurrent.get(Calendar.MONTH),
                        mCurrent.get(Calendar.DAY_OF_MONTH)
                    );

                    dialog.setTitle(mPlayers.get(pos).get_header());
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
                    dialog.show();

                    }
                });
                break;


            //Type Time
            case 4:

                //Se agrega icono de requirido si corresponse
                prepareTitle(holder, fb);
                fb.setPos_adapter(pos);

                holder.et_value.setId(fb.get_number_col());
                holder.et_value.setInputType(InputType.TYPE_CLASS_TEXT);

                holder.et_value.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_clock,
                        0,
                        0,
                        0);

                holder.et_value.setCursorVisible(false);
                holder.et_value.setFocusable(false);

                holder.et_value.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {

                    mCurrent = Calendar.getInstance();

                    TimePickerDialog Tp = new TimePickerDialog(mContext,
                        android.R.style.Theme_Holo_Light_Dialog,
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view,
                                                  int hourOfDay,
                                                  int minute) {

                                mCurrent.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                mCurrent.set(Calendar.MINUTE, minute);

                                SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:00");

                                //Set object
                                fb.setValue( timeFormat.format(mCurrent.getTime()) );

                                //Set et
                                holder.et_value.setText( timeFormat.format(mCurrent.getTime()) );

                            }

                        },mCurrent.get(Calendar.HOUR_OF_DAY),
                        mCurrent.get(Calendar.MINUTE),
                        true);

                    Tp.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    Tp.show();

                    }
                });

                break;



            //Type datetime
            case 5:

                //Se agrega icono de requirido si corresponse
                prepareTitle(holder, fb);
                fb.setPos_adapter(pos);

                holder.et_value.setId(fb.get_number_col());
                holder.et_value.setInputType(InputType.TYPE_CLASS_TEXT);

                holder.et_value.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_datetime,
                        0,
                        0,
                        0);

                holder.et_value.setCursorVisible(false);
                holder.et_value.setFocusable(false);

                holder.et_value.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    mCurrent = Calendar.getInstance();

                    DatePickerDialog dialogDate = new DatePickerDialog(mContext,
                        android.R.style.Theme_Holo_Light_Dialog,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                            mCurrent.set(year, monthOfYear, dayOfMonth);

                            TimePickerDialog dialogTime = new TimePickerDialog(mContext,
                                    android.R.style.Theme_Holo_Light_Dialog,
                                    new TimePickerDialog.OnTimeSetListener() {

                                        @Override
                                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                                        mCurrent.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                        mCurrent.set(Calendar.MINUTE, minute);

                                        SimpleDateFormat dateFormat_obj = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
                                        SimpleDateFormat dateFormat_et  = new SimpleDateFormat("dd-MM-yyyy HH:mm:00");

                                        //Set object
                                        fb.setValue( dateFormat_obj.format(mCurrent.getTime()) );

                                        //Set et
                                        holder.et_value.setText( dateFormat_et.format(mCurrent.getTime()) );


                                        }
                                    },      mCurrent.get(Calendar.HOUR_OF_DAY),
                                    mCurrent.get(Calendar.MINUTE),
                                    true);

                            dialogTime.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                            dialogTime.setTitle(mPlayers.get(pos).get_header());
                            dialogTime.show();

                            }

                        },
                        mCurrent.get(Calendar.YEAR),
                        mCurrent.get(Calendar.MONTH),
                        mCurrent.get(Calendar.DATE)
                    );

                    dialogDate.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    dialogDate.setTitle(mPlayers.get(pos).get_header());
                    dialogDate.show();

                    }
                });

                break;


            //Type radio buttom
            case 6:

                //Se agrega icono de requirido si corresponse
                prepareTitle(holder, fb);
                fb.setPos_adapter(pos);

                holder.et_value.setId(fb.get_number_col());
                holder.et_value.setInputType(InputType.TYPE_CLASS_NUMBER);
                holder.et_value.setCursorVisible(false);
                holder.et_value.setFocusable(false);

                holder.et_value.setText("SI"); //crear el defult_value

                holder.et_value.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_radio_button,
                        0,
                        0,
                        0);

                holder.et_value.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    LayoutInflater inflater = LayoutInflater.from(mContext);
                    View dialoglayout       = inflater.inflate(R.layout.rbtm_dialog, null);
                    RadioGroup radioGroup   = (RadioGroup) dialoglayout.findViewById(R.id.item_rbtn);

                    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup radioGroup, int i) {
                        switch (i){
                            case R.id.rb_1:
                                fb.setValue("SI");
                                holder.et_value.setText( "SI" );
                                break;
                            case R.id.rb_2:
                                fb.setValue("NO");
                                holder.et_value.setText( "NO" );
                                break;
                            case R.id.rb_3:
                                fb.setValue("N/A");
                                holder.et_value.setText( "N/A" );
                                break;
                        }
                        }
                    });

                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    builder.setTitle(fb.get_header());
                    builder.setView(dialoglayout);
                    builder.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });


                    if (holder.et_value.getText().toString().equals("SI")){
                        radioGroup.check(R.id.rb_1);
                    }else if (holder.et_value.getText().toString().equals("NO")){
                        radioGroup.check(R.id.rb_2);
                    }else{
                        radioGroup.check(R.id.rb_3);
                    }

                    builder.show();

                    }
                });
                break;



            //Type draw
            case 7:

                //Se agrega icono de requirido si corresponse
                prepareTitle(holder, fb);
                fb.setPos_adapter(pos);

                holder.et_value.setId(fb.get_number_col());
                holder.et_value.setCursorVisible(false);
                holder.et_value.setFocusable(false);

                holder.et_value.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_edit,
                        0,
                        0,
                        0);

                holder.et_value.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    AlertDialog.Builder quest = new AlertDialog.Builder(mContext);
                    quest.setTitle(R.string.app_name);
                    quest.setIcon(R.mipmap.ic_launcher);
                    quest.setMessage(R.string.question_media);

                    quest.setPositiveButton("Nuevo", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        Intent intent = new Intent(mContext, draw.class);
                        intent.putExtra("fb", fb);

                        ((Activity) mContext).startActivityForResult(intent,
                                0);

                        }
                    });


                    if (fb.getValue() != null){
                        quest.setNegativeButton("Ver", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                try {
                                    Intent intent = new Intent();
                                    intent.setAction(Intent.ACTION_VIEW);
                                    intent.setDataAndType(Uri.parse("file://" + new Const().MEDIA + fb.getValue()), "image/*");
                                    mContext.startActivity(intent);
                                } catch (ActivityNotFoundException anfe) {
                                }

                            }
                        });
                    }
                    quest.show();

                    }
                });

                break;

            //Type camare
            case 8:

                //Se agrega icono de requirido si corresponse
                prepareTitle(holder, fb);
                fb.setPos_adapter(pos);

                holder.et_value.setId(fb.get_number_col());
                holder.et_value.setCursorVisible(false);
                holder.et_value.setFocusable(false);

                holder.et_value.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_camera,
                        0,
                        0,
                        0);

                holder.et_value.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    AlertDialog.Builder quest = new AlertDialog.Builder(mContext);

                    quest.setTitle  (R.string.app_name);
                    quest.setIcon   (R.mipmap.ic_launcher);
                    quest.setMessage(R.string.question_media);


                    quest.setPositiveButton("Camara", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            Intent intent = new Intent(mContext, talkphoto.class);
                            intent.putExtra("fb", fb);
                            ((Activity) mContext).startActivityForResult(intent,
                                    1);

                        }
                    });


                    quest.setNegativeButton("Galeria", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            Intent intent = new Intent(mContext, getphoto.class);
                            intent.putExtra("fb", fb);

                            ((Activity) mContext).startActivityForResult(intent,
                                    2);

                        }
                    });


                    if (mPlayers.get(pos).getValue()!=null){

                        quest.setNeutralButton("Ver", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                File f = new File(new Const().MEDIA + fb.getValue());
                                Uri uri = Uri.fromFile(f);
                                intent.setDataAndType(Uri.parse(String.valueOf(uri)),"image/*");
                                mContext.startActivity(intent);

                            }
                        });
                    }

                    quest.show();

                    }
                });
                break;



            //Type list
            case 9:

                //Se agrega icono de requirido si corresponse
                prepareTitle(holder, fb);
                fb.setPos_adapter(pos);

                holder.et_value.setId(fb.get_number_col());
                holder.et_value.setCursorVisible(false);
                holder.et_value.setFocusable(false);

                holder.et_value.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Intent intent = new Intent(mContext, list.class);
                        intent.putExtra("fb",fb);
                        ((Activity) mContext).startActivityForResult(intent,
                                3);

                    }
                });
                break;

        }

    }




    @SuppressLint("NewApi")
    private void prepareTitle(ViewHolder holder,
                              FormBody fb){

        //Set text del title
        holder.tv_title.setText(" " + fb.get_header());

        //¿Campos requiredios o no?
        if (fb.is_required()){

            holder.tv_title.setCompoundDrawablesRelativeWithIntrinsicBounds(
                    mContext.getResources().getDrawable(
                            R.drawable.ic_icon_required),
                    null,
                    null,
                    null);

        }
    }
}

Y así es como lo invoco de la main activity

    adapter = new rvadapter(main.this, getPlayers());
    mLayoutManager = new LinearLayoutManager(this);
    rv.setLayoutManager(mLayoutManager);
    rv.setAdapter(adapter);
    rv.setSaveEnabled(true);
4
  • Hola Christian, específicamente que valores se están cambiando?
    – Jorgesys
    Commented el 1 dic. 2017 a las 19:36
  • Hola buenas tardes y gracias por preguntar, deje un video en la descripción del problema, los campos de texto cambian su valor y me ha ocurrido que el orden en que se cambian puede ser distinto cada vez.
    – C.Meza
    Commented el 1 dic. 2017 a las 19:46
  • Type Camera que caso es?
    – Jorgesys
    Commented el 1 dic. 2017 a las 19:51
  • Type Camera es el número 8
    – C.Meza
    Commented el 1 dic. 2017 a las 19:52

2 respuestas 2

0

Estas usando solo una instancia de TextView y EditText, el problema es que el tipo 2 esta cambiando el valor de fb, de esta forma:

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        fb.setValue( holder.et_value.getText().toString() );
                    }

Cuando trata de obtener los valores para el tipo 8, que es el nombre del archivo que guardaste, el nombre que obtiene es incorrecto:

   File f = new File(new Const().MEDIA + fb.getValue());
1
  • Pero fb guarda la posición de cada item ejemplo: mPlayers.get(pos) como es posible que cambien su valor?
    – C.Meza
    Commented el 1 dic. 2017 a las 21:17
0

vi esta sugerencia en la web y me solucionó el problema. Se debe incluir la siguiente línea dentro de la definición de "private void ui(View v) {..."

recyclerView.setItemViewCacheSize(preguntasList.size());

En mi caso la puse al final.

Saludos

1
  • 1
    private void ui( ) no existe para sobreescribir, ¿es unmétodo del adapter?
    – Jorgesys
    Commented el 24 oct. 2019 a las 14:26

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.