简体   繁体   中英

EditText loses content to move the listview

I have a problem with my EditText in the scrolling list, the contents of the EditText gets cluttered, so I read it because using less memory is re-assigned IDs. I have read the same query and answers about this topic but I can not solve.

I would be very grateful for your help, I'm new programming for mobile devices

This is my code:

public class AdapterFilaProducto extends BaseAdapter {
  protected Activity activity;
  protected ArrayList<ItemFilaProducto> items;
  protected ItemFilaProducto item;
  protected String tipo;
  protected Mascaras msk = new Mascaras();
  public ImageButton btnImDerecha ;
  public ImageButton btnImIzquierda;
  public TextView producto;
  private InterfazSeleccionProducto contex;  
  private BaseDataAdapter db;
  private TextView precioCantidad;
      TextView precio;

  public AdapterFilaProducto(Activity activity, ArrayList<ItemFilaProducto> items,String tipo) {
    this.activity = activity;
    this.items = items;
    this.tipo = tipo;
  }
  public AdapterFilaProducto(Activity activity,InterfazSeleccionProducto contex, ArrayList<ItemFilaProducto> items,String tipo) {
        this.activity = activity;
        this.items = items;
        this.tipo = tipo;
        this.contex=contex;
      }

  public int getCount() {
    return items.size();
  }

  public Object getItem(int position) {
    return items.get(position);
  }

  public long getItemId(int position) {
    return items.get(position).getId();
  }
  public String getItemProducto(int position) {
        return items.get(position).getProducto();
      }
  public String getItemCantidad(int position) {
        return items.get(position).getCantidad();
      }
  public String getItemPercio(int position) {
        return items.get(position).getPrecio();
      }
  public EditText getItemEdit(int position) {
        return items.get(position).getEditTxt();
      }

  public View getView(int position, View convertView, ViewGroup parent) 
  {
    db = new BaseDataAdapter(activity);

    View vi=convertView;
    final ItemFilaProducto item = items.get(position);

    if(convertView == null) 
    {
              LayoutInflater inflater = (LayoutInflater)   activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              vi = inflater.inflate(R.layout.list_producto, null);
    }  

       btnImDerecha = (ImageButton) vi.findViewById(R.id.BtnDerechaVenta);
       btnImIzquierda = (ImageButton) vi.findViewById(R.id.BtnIzquierdaVenta);
       TextView producto = (TextView) vi.findViewById(R.id.txtProductoVenta);
       item.precio = (TextView) vi.findViewById(R.id.txtCantidadVenta);
       item.edtxt = (EditText) vi.findViewById(R.id.editTxtListVenta);
       producto.setText(item.getProducto());
       Log.i("Precio",item.montoPrecio);
       if(item.cantidad.equalsIgnoreCase("0")){
           item.precio .setText(item.Precio);
       }else 
           item.precio .setText(item.montoPrecio);

       item.edtxt.setText(""+item.cantidad);

       btnImDerecha.setOnClickListener(new OnClickListener() {

             @Override
                public void onClick(View v) {
                    int i = Integer.parseInt(item.edtxt.getText().toString());
                    i=i+1;
                    item.edtxt.setText(""+i);

             }
            });
       btnImIzquierda.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    int i = Integer.parseInt(item.edtxt.getText().toString());
                      if(0<i){
                        i=i-1;
                        item.edtxt.setText(""+i);
                        }
             }
            });
               item.edtxt.addTextChangedListener(new TextWatcher() {
               public void afterTextChanged(Editable s)
               { 

               }
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {


            }
            public void onTextChanged(CharSequence s, int start, int before, int count) { 
                db.open();
                Cursor cur = db.obtenerProductosTipo(tipo,item.getIdPro());
                if(cur.moveToFirst()){
                    int precio = cur.getInt(3);
                    int cantidad = Integer.parseInt(item.edtxt.getText().toString());
                    int monto = precio*cantidad;
                    if(0 < monto){
                        item.setPrecio(msk.mascaraMontoTxt(String.valueOf(monto)));
                        item.precio .setText(item.getPrecio());
                    }
                    db.actualizarProductoMonto(item.getIdPro(),monto);
                    db.actualizarProductoCantidad(item.getIdPro(),cantidad);
                }
                cur.close();
                int montoTotal = 0;
                Cursor curAll = db.obtenerProductosTipo(tipo);
                if(curAll.moveToFirst()){

                    do{
                        montoTotal = montoTotal + curAll.getInt(5);
                        Log.e("CANTIDAD", ""+curAll.getInt(5));
                    }while(curAll.moveToNext());
                    }

                curAll.close();
                try{
                    db.borrarTablaPar("MONTO");
                }catch(Exception e){

                }
                Log.e("MONTO", ""+montoTotal);
                DtoParametro dtoParametro = new DtoParametro();
                dtoParametro.ID_PAR = "MONTO";
                dtoParametro.VALOR = String.valueOf(montoTotal);
                Log.i("MONTO",""+String.valueOf(montoTotal));
                db.insertarParametro(dtoParametro);
                db.close();
                contex.mostrarMonto();          
            }
            });
    return vi;
  }

}

ListViews reuse their rows, so you need to keep track of which EditTexts contain what and restore them as the ListView scrolls. Simply create a new List<String> in your adapter, I'll call it userStrings , to restore the appropriate values use:

item.edtext.setText(userStrings.get(position));

And store these values in onTextChanged() or when the EditText loses focus.

Though I noticed that your current Adapter can be sped up a bit by using ViewHolders and initializing your code only when new layouts are inflated. This is discussed in details in various Google I/O talks like Turbo-Charge Your UI and World of LiistView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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