2016-04-05 17 views
-2

をそれ自身を複製。どうすればそれを防ぐことができますか?値は、私は私はそれが<code>ListView</code>のすべての4行目に自分自身を複製<code>EditText</code>に値を入力毎row.Whenで<code>EditText</code>を有する<code>ListView</code>を有するすべての4行目に

public class ItemsAdapter extends ArrayAdapter<Items>{ 


    Double Price; 
    Double Quantity; 
    String _Quantity; 
    private int resource; 
    private LayoutInflater inflater; 
    private Context context; 
    Items items; 


    public ItemsAdapter(Context ctx, int resourceId, List<Items> objects){ 

     super(ctx, resourceId, objects); 
     resource = resourceId; 
     inflater = LayoutInflater.from(ctx); 
     context=ctx; 

    } 


    @Override 
    public View getView (int position, View convertView, ViewGroup parent) { 

     ItemViewHolder viewHolder; 
     items = getItem(position); 

     if (convertView == null) { 

      viewHolder=new ItemViewHolder(); 
      convertView = (RelativeLayout) inflater.inflate(resource, null); 

    viewHolder.txtQuantity =(EditText)convertView.findViewById(R.id.txtQuantity); 
      viewHolder.lblItemsName=(TextView) convertView.findViewById(R.id.lblItemName); 
      viewHolder.lblPrice = (TextView) convertView.findViewById(R.id.lblPrice); 

     } else { 

      viewHolder = (ItemViewHolder) convertView.getTag(); 
     } 


     viewHolder.lblItemsName.setText(items.getItemName()); 
     viewHolder.lblPrice.setText(items.getPrice().toString()); 

     return convertView; 
    } 


    public class ItemViewHolder { 

    public EditText txtQuantity ; 
    public TextView lblItemsName; 
    public TextView lblPrice; 

    } 
}` 

答えて

0

私は

public ItemAdapter(Context context, int textViewResourceId, 
           ArrayList<Cart> productList) { 
      super(context, textViewResourceId, productList); 
      this.productList = new ArrayList<Cart>(); 
      this.productList.addAll(productList); 
     } 

     @Override 
     public int getCount() { 
      return this.productList.size(); 
     }; 
     @Override 
     public View getView(final int position, View view, ViewGroup parent) { 

      DecimalFormat df = new DecimalFormat("0.00##"); 

     //  productList= DBAdapter.getCartList(); 
      final  Cart product = productList.get(position); 



      if (view == null) { 
       LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       view = vi.inflate(R.layout.list_cart, null); 


       EditText quantity = (EditText) view.findViewById(R.id.txtQuantity); 


       //attach the TextWatcher listener to the EditText 
       quantity.addTextChangedListener(new MyTextWatcher(view)); 

      } 


      TextView ItemNumber=(TextView)view.findViewById(R.id.itemNumber); 
      ItemNumber.setVisibility(View.GONE); 



      EditText quantity = (EditText) view.findViewById(R.id.txtQuantity); 
      quantity.setTag(product); 
      if(product.getQuantity() != 0){ 
       quantity.setText(String.valueOf(product.getQuantity())); 
      } 
      else { 
       quantity.setText("1"); 
      } 

      TextView itemNumber = (TextView) view.findViewById(R.id.itemNumber); 
      itemNumber.setText(product.getItemNumber()); 
      TextView ItemName = (TextView) view.findViewById(R.id.lblItemName); 
      ItemName.setText(product.getItemName()); 
      TextView price = (TextView) view.findViewById(R.id.lblPrice); 
      price.setText(df.format(product.getPrice())); 
      TextView ext = (TextView) view.findViewById(R.id.lblTotal); 
      if(product.getQuantity() != 0){ 
       ext.setText("$" + df.format(product.getTotal())); 
      } 
      else { 
       ext.setText(""); 
      } 






      return view; 

     } 

    } 

    private class MyTextWatcher implements TextWatcher { 

     private View view; 
     private MyTextWatcher(View view) { 
      this.view = view; 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      //do nothing 
     } 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      //do nothing 
     } 
     public void afterTextChanged(Editable s) { 

      DecimalFormat df = new DecimalFormat("0.00##"); 
      String qtyString = s.toString().trim(); 
      int quantity = qtyString.equals("") ? 0:Integer.valueOf(qtyString); 

      EditText qtyView = (EditText) view.findViewById(R.id.txtQuantity); 
      Cart product = (Cart) qtyView.getTag(); 

      if(product.getQuantity() != quantity){ 

       Double currPrice = product.getTotal(); 
       Double extPrice = quantity * product.getPrice(); 
       Double priceDiff = Double.valueOf(df.format(extPrice - currPrice)); 

       product.setQuantity(quantity); 
       product.setTotal(extPrice); 

       TextView ext = (TextView) view.findViewById(R.id.lblTotal); 
       if(product.getQuantity() != 0){ 
        ext.setText("$" + df.format(product.getTotal())); 
       } 
       else { 
        ext.setText(""); 
       } 

       if(product.getQuantity() != 0){ 
        qtyView.setText(String.valueOf(product.getQuantity())); 
       } 
       else { 
        qtyView.setText(""); 
       } 

       orderTotal += priceDiff; 
       TextView cartTotal = (TextView) findViewById(R.id.cartTotal); 
       cartTotal.setText(df.format(orderTotal)); 

      } 

      return; 
     } 
    } 
0

あなたがListViewの各セルに数量を入力して起動すると、あなたはその後items に数量を保存する必要があり、あなたはitem

... your above code 
    } 

     viewHolder.txtQuantity.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) {} 
       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

       @Override 
       public void afterTextChanged(Editable s) { 
         items.setQuantity(s); // set quantity for each item when you enter quantity 
       } 
      }); 

    viewHolder.lblItemsName.setText(items.getItemName()); 
    viewHolder.lblPrice.setText(items.getPrice().toString()); 

    viewHolder.txtQuantity.setText(items.getQuantity()); // load quantity for each item 
    ... 

から数量をロード

+0

考えるための良い方法をそれを把握するが、それは問題を解決するためのdidnt –

0
このヘルプホープあなたがスクロールするとビューが Listviewでリサイクルされているので、

重複が発生します。これを避けるために、あなたはgetView()方法でEdittextのテキストをリセットする必要があります。

あなたはファンティエットVANたLiNHが提案のように、あなたが何かを行う必要があり、再び戻っビューに来たときにEditTextのテキストを保持する場合。

関連する問題