自從我正式開始學習代碼之後,我剛剛進入第三個月,這一直很有趣。 目前,我正在嘗試使用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代碼以啓用這些計算並顯示總金額。 我知道設置點擊監聽器將是必需的,但我不確定正確的方式來設置它們,因爲用戶需要點擊多個位置,即按鈕,同時設置要購買的產品數量。 我已經花了最後五天尋找答案,但在我得到的所有答案中,有一些缺失導致應用程序有錯誤或不做任何事情。 我期待着您就如何最好地解決這一挑戰提出的建議。 預先感謝您。
謝謝,讓我試試看。 – Mika369