2016-04-11 26 views
0

İçinde dört EditText içeren bir ListView var. Bu alanlarda bir metin girdiğimde ve sayfayı kaydırdığımda, verileri kaybettim. Çok fazla örnek denedim ama hiçbir şey benim için işe yaramadı.Lütfen yardım edin.EditText in ListView Kaydırma sorunu giderilemiyor

public NewOrderTotalCountAdapter(Context context, int resource, 
     List<Object> objList) { 
    super(context, resource, objList); 
    this.context = context; 
    this.resourceId = resource; 
    this.objArrayList = objList; 
    Log.i("Size============", String.valueOf(objList.size())); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View totalView = convertView; 
    TotalCountHolder totalCountHolder; 
    if (totalView == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     totalView = inflater.inflate(resourceId, parent, false); 

     totalCountHolder = new TotalCountHolder(); 
     totalCountHolder.productsNameView = (EditText) totalView 
       .findViewById(R.id.productName); 
     totalCountHolder.productQuantityView = (EditText) totalView 
       .findViewById(R.id.quantity); 
     totalCountHolder.productUnitPriceView = (EditText) totalView 
       .findViewById(R.id.unitPrice); 
     totalCountHolder.productTotalPriceView = (EditText) totalView 
       .findViewById(R.id.totalPrice); 
     totalCountHolder.btnDel = (Button) totalView 
       .findViewById(R.id.btnDel); 
     totalCountHolder.productsNameView 
     .addTextChangedListener(new TextWatchers(
     totalCountHolder.productsNameView, position)); 
     totalCountHolder.productQuantityView 
     .addTextChangedListener(new TextWatchers(
     totalCountHolder.productQuantityView, position)); 
     totalCountHolder.productUnitPriceView 
     .addTextChangedListener(new TextWatchers(
     totalCountHolder.productUnitPriceView, position)); 
     totalCountHolder.productTotalPriceView 
     .addTextChangedListener(new TextWatchers(
     totalCountHolder.productTotalPriceView, position)); 

     totalCountHolder.btnDel.setTag(position); 
     totalCountHolder.btnDel.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Integer index = (Integer) v.getTag(); 

       objArrayList.remove(index.intValue()); 
       notifyDataSetChanged(); 
      } 
     }); 
     totalView.setTag(totalCountHolder); 

    } else { 
     totalCountHolder = (TotalCountHolder) totalView.getTag(); 
    } 
    try { 

     if (objArrayList != null) { 
      if (position < objArrayList.size()) { 
       ProductsEntity productEntity = (ProductsEntity) objArrayList 
         .get(position); 
       String productName = productEntity.getProcdut_name(); 
       String quantity = productEntity.getQuantity(); 
       String unitPrice = productEntity.getUnit_price(); 
       String total_price = productEntity.getTotal_price(); 
       totalCountHolder.productsNameView.setText(productName); 
       totalCountHolder.productQuantityView.setText(quantity); 
       totalCountHolder.productUnitPriceView.setText(unitPrice); 
       totalCountHolder.productTotalPriceView.setText(total_price); 
       totalCountHolder.btnDel.setVisibility(View.VISIBLE); 

      } else { 

      totalCountHolder.productsNameView.setText(""); 
      totalCountHolder.productQuantityView.setText(""); 
      totalCountHolder.productUnitPriceView.setText(""); 

      totalCountHolder.productTotalPriceView.setText(""); 
       totalCountHolder.btnDel.setVisibility(View.GONE); 
      } 

     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return totalView; 
} 

@Override 
public int getCount() { 
    int i = objArrayList.size(); 
    int j = i + 2; 
    return objArrayList != null ? j : 0; 

} 

private class TotalCountHolder { 
    EditText productsNameView; 
    EditText productQuantityView; 
    EditText productUnitPriceView; 
    EditText productTotalPriceView; 
    Button btnDel; 
} 

class TextWatchers implements TextWatcher { 
    View view; 
    int position; 

    TextWatchers(View view, int position) { 
     this.view = view; 
     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) { 

    } 

    @Override 
    public void afterTextChanged(Editable s) { 

     EditText et = (EditText) view; 

     if (view.getId() == R.id.productName) { 
      ProductsEntity productEntity = (ProductsEntity) objArrayList 
        .get(position); 
      String name = et.getText().toString().trim(); 
      productEntity.setProcdut_name(name); 
      //objArrayList.set(position, productEntity); 
      Log.i("Name====", name); 
      Log.i("Product Entity====", productEntity.toString()); 
      return; 

     } 
     if (view.getId() == R.id.quantity) { 
      ProductsEntity productEntity = (ProductsEntity) objArrayList 
        .get(position); 
      String name = et.getText().toString().trim(); 
      productEntity.setQuantity(name); 
      //objArrayList.set(position, productEntity); 
      Log.i("Name====", name); 
      Log.i("Product Entity====", productEntity.toString()); 
      return; 

     } 
     if (view.getId() == R.id.unitPrice) { 
      ProductsEntity productEntity = (ProductsEntity) objArrayList 
        .get(position); 
      String name = et.getText().toString().trim(); 
      productEntity.setUnit_price(name); 
      //objArrayList.set(position, productEntity); 
      Log.i("Name====", name); 
      Log.i("Product Entity====", productEntity.toString()); 
      return; 

     } 
     if (view.getId() == R.id.totalPrice) { 
      ProductsEntity productEntity = (ProductsEntity) objArrayList 
        .get(position); 
      String name = et.getText().toString().trim(); 
      productEntity.setTotal_price(name); 
      //objArrayList.set(position, productEntity); 
      Log.i("Name====", name); 
      Log.i("Product Entity====", productEntity.toString()); 
      return; 

     } 

    } 

} 

aşağı kaydırdığınızda veya varsayılan android sistem her yeni satır oluşturur ve bu nedenle önceki satırları imha ederek kaydırırken liste görünümü kadar, verilerinizi kaybedince Bu benim Adaptör sınıfı

+0

bu bağlantıya bakın http://stackoverflow.com/questions/35967787/edittext-in-listview-gets-duplicated –

cevap

0

olduğunu. PaylaşılanPreferences'daki verileri, satırın konumu olarak anahtar ile kaydetmeyi ve daha sonra paylaşılan paylaşımlardan verileri almayı deneyin. Her zaman, mevcut konumun önceden paylaşılmış olsa da, getView() yönteminin başlangıcını kontrol etmeniz gerekir. Aksi takdirde edittext değerini bulamazsanız bulun.

İlgili konular