2017-02-23 25 views
-3

我已經創建了一個列表視圖,其中包含每個列表項的產品名稱,價格和數量。我想獲得總數量的總和,但我無法這樣做。如何在Android中添加列表視圖的所有列的值?

我該怎麼辦?

我一直在使用產品列表中使用的代碼:適配器代碼

私人無效setTotalQuantity(TextView中的TextView) {

for(int i = 0 ; i< dataSet.size();i++) 
    { 


     totalquantity =   totalquantity+Integer.parseInt(dataSet.get(i).getProductQuantity()); 

     totalbill = totalbill+Integer.parseInt(dataSet.get(i).getProductPrice()) * Integer.parseInt(dataSet.get(i).getProductQuantity()); 
    } 

} 

//加或減按鈕

 private void subtractQuantity(final ImageView imageView, final TextView textView) { 

    String qty = textView.getText().toString(); 

    if (Integer.parseInt(qty) == 0) 

    { 
     imageView.setClickable(false); 
    } else { 
     imageView.setClickable(true); 
     qty = Integer.parseInt(qty) - 1 + ""; 
     textView.setText(qty); 
     data.setProductQuantity(qty); 
     setTotalQuantity(txtQtyAll); 
    } 

} 


private void AddQuantity(final ImageView add, final TextView textView, final ImageView minus) { 

    String qty = textView.getText().toString(); 


    qty = Integer.parseInt(qty) + 1 + ""; 

    textView.setText(qty); 
    data.setProductQuantity(qty); 
    setTotalQuantity(txtQtyAll); 
    if(Integer.parseInt(qty) >0) 
    { 
     minus.setClickable(true); 
    } 


} 

越來越空指針異常

http://prntscr.com/ecau0x

//更新的代碼

 private void subtractQuantity(final ImageView imageView, final TextView textView) { 

    String qty = textView.getText().toString(); 

    if (Integer.parseInt(qty) == 0) 

    { 
     imageView.setClickable(false); 
    } else { 
     imageView.setClickable(true); 
     qty = Integer.parseInt(qty) - 1 + ""; 
     textView.setText(qty); 
     data.setProductQuantity(qty); 
     setTotalQuantity(txtQtyAll); 
    } 

} 

 public AdapterProductListing(List<ProductModel> dataModels, Context context, TextView quantity) { 
    this.dataSet = dataModels; 
    this.mContext = context; 
    this.txtQtyAll = quantity; 


} 

txtQtyAll是我從活動得到了TextView的

+1

總和量的?那麼爲什麼你使用getProductPrice()? –

+0

請提供相關代碼並在提問中予以明確。正如你在這裏要求的數量那麼這個代碼需要什麼。 –

+0

我剛用它來檢查@ ZakiPathan –

回答

0

試試這個

int totalQuantity=0; 
for(int i = 0 ; i<products.size();i++) 
    totalQuantity+=Integer.valueOf(products.get(i).getQuantity()); 
} 
0

這將如果你展示了所涉及的所有代碼,就會更容易。你是否想要獲得所有產品的總價?

假設你初始化「INT總」已經和方法getProductPrice()是好的,你可以這樣做:

total += Integer.parseInt(products.get(i).getProductPrice()); 

檢查錯誤日誌爲好。希望這可以幫助。

0
for(int i = 0 ; i<products.size();i++) 
{ 
    totalprice = totalprice+Integer.parseInt(products.get(i).getProductPrice()); 

totalquantity = totalquantity+Integer.parseInt(products.get(i).getProductQuantity()); 

totalbill = totalbill+Integer.parseInt(products.get(i).getProductPrice() * products.get(i).getProductQuantity()); 
} 

試試這個

+0

我想問你多一個查詢我,我有一個適配器和一個列表視圖活動,所以我應該使用此代碼,以便它將不斷更新,因爲每個項目添加或刪除過程正在進行。因爲我使用了相同的代碼,但無法連續更新。 –

+0

在getview上的適配器代碼 –

+0

我試過但得到空指針異常我要更新問題再次檢查它 –

相關問題