How to update adapter data when editing text from a recyclerview

1

Good afternoon, I'm new to the forum so I apologize if I'm wrong with the question or it's a very silly question.

It turns out that I am making an application and I have a recyclerview where it has a edittext which carries the quantity of products that I am going to request, until then everything is fine, only when I edit the quantity, it does not store it once in the adapter but I stay in the view until I add a new element that runs the adapter I stored, this is my CustomAdapter

public class AdaptadorPedido extends 
RecyclerView.Adapter<AdaptadorPedido.MyHolder> {

Context context;
List<DataAdapter> datos;
String[] etValArr;
String[] Fin;

public AdaptadorPedido(Context context, List<DataAdapter> datos) {
    this.context = context;
    this.datos = datos;
    etValArr = new String[datos.size()];
    Fin = new String[datos.size()];
}

@Override
//public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.row_view, parent, false);
    MyHolder mh = new MyHolder(view, new CustomEtListener());

    return mh;
    //return new MyHolder(view);
}

@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
    holder.Nproducto.setText(datos.get(position).getNombre());
    holder.Lab.setText(datos.get(position).getLabo());
    holder.Lista.setText(datos.get(position).getList());
    holder.precio.setText(datos.get(position).getPre());
    holder.Cantidad.setText(datos.get(position).getCant());

    holder.myCustomEtListener.updatePosition(position);
    holder.Cantidad.setText(etValArr[position]);



    holder.Nproducto.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
            adb.setTitle("Eliminar?");
            adb.setMessage("¿Seguro que desea sacar este producto de la OP ?");
            adb.setNegativeButton("No",null);
            adb.setPositiveButton("Si",new AlertDialog.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    datos.remove(position);
                    notifyDataSetChanged();
                }
            });
            adb.show();
            return false;
        }
    });
    holder.Lab.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
            adb.setTitle("Eliminar?");
            adb.setMessage("¿Seguro que desea sacar este producto de la OP ?");
            final int positionToRemove = position;
            adb.setNegativeButton("No",null);
            adb.setPositiveButton("Si",new AlertDialog.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    datos.remove(position);
                    notifyDataSetChanged();
                }
            });
            adb.show();
            return false;
        }
    });

}

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


public class MyHolder extends RecyclerView.ViewHolder{

    TextView Nproducto, Lab, Lista, precio;
    EditText Cantidad;
    public CustomEtListener myCustomEtListener;

    //public MyHolder(View itemView) {
    public MyHolder(View itemView, CustomEtListener myList) {
        super(itemView);

        Nproducto = (TextView)itemView.findViewById(R.id.tv_Producto);
        Lab = (TextView)itemView.findViewById(R.id.tv_Lab);
        Lista = (TextView)itemView.findViewById(R.id.tv_Lista);
        precio = (TextView)itemView.findViewById(R.id.tv_Precio);
        Cantidad = (EditText)itemView.findViewById(R.id.et_cant);

        myCustomEtListener = myList;
        Cantidad.addTextChangedListener(myCustomEtListener);
    }
}

public static class DataAdapter{
    String Nombre,Labo, List, Pre, Cant;

    public DataAdapter(String Nombre,String Labo,String List,String Pre,String Cant){
        this.Nombre = Nombre;
        this.Labo = Labo;
        this.List = List;
        this.Pre = Pre;
        this.Cant = Cant;
    }

    public String getNombre() {
        return Nombre;
    }

    public void setNombre(String nombre) {
        Nombre = nombre;
    }

    public String getLabo() {
        return Labo;
    }

    public void setLabo(String labo) {
        Labo = labo;
    }

    public String getList() {
        return List;
    }

    public void setList(String list) {
        List = list;
    }

    public String getPre() {
        return Pre;
    }

    public void setPre(String pre) {
        Pre = pre;
    }

    public String getCant() {
        return Cant;
    }

    public void setCant(String cant) {
        Cant = cant;
    }
}

private class CustomEtListener implements TextWatcher{
    private int position;

    public void updatePosition(int position){
        this.position = position;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        etValArr[position] = s.toString();
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
}

}

I appreciate your help.

    
asked by Diego Andres Tovar Cardozo 08.05.2017 в 23:50
source

1 answer

0

Welcome to SO in Spanish. This can be simple, when making a change to the text within the EditText , you can change the value of the Amount field in its corresponding item in the list of objects DataAdapter . It would be done in this way:

  @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //etValArr[position] = s.toString();
        datos.get(position).setCant(s.toString());
    }

When loading the data again in the RecyclerView of the corresponding element, it will load the new value.

    
answered by 09.05.2017 / 00:00
source