2017-05-05 42 views
0

自從我正式開始學習代碼之後,我剛剛進入第三個月,這一直很有趣。 目前,我正在嘗試使用android studio創建一個簡單的銷售管理android應用程序,並使用我獲得的簡單數據庫示例代碼。 我停留在建立正確的Java代碼來實現以下幾點:在由數據庫填充的項目上設置點擊監聽器

  • 用戶選擇產品通過點擊一個複選框(@ + ID /選擇)購買
  • 他進入產品的數量購買示例5(@ + id/quantity)
  • 該應用程序應該將產品價格(@ + id/price)與數量相乘並顯示總數(@ + id/total)。

對個別項目的格式如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="@dimen/activity_margin"> 
    <CheckBox 
     android:id="@+id/choice" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" /> 
    <TextView 
     android:id="@+id/name" 
     android:layout_width="70dp" 
     android:layout_height="match_parent" 
     android:text="Soda" /> 
    <TextView 
     android:id="@+id/price" 
     android:layout_width="70dp" 
     android:layout_height="match_parent" 
     android:text="1000/=" /> 
    <TextView 
     android:layout_width="10dp" 
     android:layout_height="match_parent" 
     android:text="*" /> 
    <EditText 
     android:id="@+id/quantity" 
     android:layout_width="80dp" 
     android:layout_height="match_parent" 
     android:hint="amount" 
     android:inputType="number" /> 
    <TextView 
     android:layout_width="10dp" 
     android:layout_height="match_parent" 
     android:text="=" /> 
    <TextView 
     android:id="@+id/total" 
     android:layout_width="80dp" 
     android:layout_height="match_parent" 
     android:hint="total" /> 
</LinearLayout> 

(我已刪除了一些拋光代碼,從而縮短代碼,並仍顯示最重要的部分)。 產品的名稱和價格由SQLite數據庫填充,該數據庫使用通常的Java文件(如Contract,Dbhelper,Provider和Cursor Adapter)創建。 用戶將在另一個活動輸入產品名稱和價格,所有的產品甚至超過50個產品多,會使用以下列表視圖佈局格式填充:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".SalesActivity2"> 
    <ListView 
     android:id="@+id/sales_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </ListView> 
</RelativeLayout> 

到目前爲止,一切工作正常。用戶可以輸入儘可能多的產品。數據庫能夠按照格式和佈局在列表視圖上填充產品。 挑戰在於在SalesActivity2中設置正確的Java代碼以啓用這些計算並顯示總金額。 我知道設置點擊監聽器將是必需的,但我不確定正確的方式來設置它們,因爲用戶需要點擊多個位置,即按鈕,同時設置要購買的產品數量。 我已經花了最後五天尋找答案,但在我得到的所有答案中,有一些缺失導致應用程序有錯誤或不做任何事情。 我期待着您就如何最好地解決這一挑戰提出的建議。 預先感謝您。

回答

0

您可以將監聽器添加到您的edittext中,該文本從用戶獲取產品數量。默認情況下,它應該爲空或禁用,當用戶選擇該產品時,其文本應該更改爲1(根據我的邏輯),並且如果用戶本身改變它的值,然後計算總量。

現在讓你添加TextWatcher該事件對你的EditText

editText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

      // TODO Auto-generated method stub 
     } 

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

      // TODO Auto-generated method stub 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

      // TODO Auto-generated method stub 
     } 
    }); 

每當用戶改變文本就可以計算出更新的總量。

+0

謝謝,讓我試試看。 – Mika369

0

添加OnCheckedChangeListenerCheckBox,展現total價格單quantity當用戶在Checkbox第一click並清除quantity & total價格當用戶un-checkCheckBox

2。添加TextChangedListenerEditText,以顯示用戶開始輸入時的總價格quantity

試試這個:

CheckBox checkBoxChoice = (CheckBox) findViewById(R.id.choice); 
    final EditText editQuantity = (EditText) findViewById(R.id.quantity); 
    final TextView textPrice = (TextView) findViewById(R.id.price); 
    final TextView textTotal = (TextView) findViewById(R.id.total); 


    checkBoxChoice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { 
      if (checked) { // Required to show total price for single quantity when user click on checkbox 
       int quantity = 1; 
       int price = Integer.parseInt(textPrice.getText().toString()); 
       int total = price * quantity; 
       textTotal.setText(String.valueOf(total)); 
      } else { // Required to clear quantity and total price when user un-check checkbox 
       editQuantity.setText("0"); 
       textTotal.setText("0"); 
      } 
     } 
    }); 

    // Required to show total price when user start typing quantity 
    editQuantity.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      int quantity = Integer.parseInt(charSequence.toString()); 
      int price = Integer.parseInt(textPrice.getText().toString()); 
      int total = price * quantity; 

      textTotal.setText(String.valueOf(total)); 
     } 

     @Override 
     public void afterTextChanged(Editable editable) { 

     } 
    }); 

僅供參考,price TextView的,請嘗試將價格只值(1000)而不是字符串(1000/=),因爲我們是從price TextView中得到的價格。

希望這會有所幫助〜

+0

當然,是用戶誰輸入價格,價值只是一個佔位符,讓我試試這個,謝謝 – Mika369

+0

這看起來好像會起作用,但我們將不得不處理它拋出的空指針異常「 checkBoxChoice.setOnCheckedChangeListener ......「。不知何故,該應用程序無法找到該視圖上覆選框的確切位置。從我迄今爲止的研究中,我試圖圍繞與列表視圖關聯的項目單擊偵聽器打包該代碼。我結束了這種情況,沒有任何錯誤,但是我點擊總文本視圖後應用程序什麼都不做。 – Mika369

+0

有些東西我們仍然在這裏失蹤,使其工作。主要列表視圖佈局的名稱是「activity_sales2」,單個項目的佈局名稱是「sales_list2」。我希望他們能幫助建立正確的代碼來獲得這些觀點。 – Mika369

相關問題