Java 在由数据库填充的项目上设置单击侦听器

Java 在由数据库填充的项目上设置单击侦听器,java,android,database,sqlite,onclick,Java,Android,Database,Sqlite,Onclick,我刚刚进入正式开始学习编码的第三个月,这是一段有趣的旅程。 目前,我正试图使用AndroidStudio从我得到的一个简单的数据库示例代码中创建一个简单的销售管理android应用程序。 我一直在设置正确的java代码以启用以下功能: 用户通过单击复选框(@+id/choice)选择要购买的产品 他输入要购买的产品数量示例5(@+id/数量) 应用程序应该将产品的价格(@+id/price)与数量相乘,并显示总数(@+id/total) 个别项目的格式如下: <?xml version

我刚刚进入正式开始学习编码的第三个月,这是一段有趣的旅程。 目前,我正试图使用AndroidStudio从我得到的一个简单的数据库示例代码中创建一个简单的销售管理android应用程序。 我一直在设置正确的java代码以启用以下功能:

  • 用户通过单击复选框(@+id/choice)选择要购买的产品
  • 他输入要购买的产品数量示例5(@+id/数量)
  • 应用程序应该将产品的价格(@+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文件(如契约、Dbhelper、提供者和游标适配器)创建的。 用户将在另一个活动中输入产品名称和价格,所有产品(甚至超过50种产品)将在以下listview布局中使用该格式填充:

<?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>

到目前为止,其他一切正常。用户可以输入任意数量的产品。数据库能够按照格式和布局填充listview上的产品。 挑战在于在SalesActivity2中设置正确的java代码以启用这些计算并显示总数。 我知道需要设置单击侦听器,但我不确定设置它们的正确方法,因为用户需要单击多个位置(即按钮),同时设置要购买的产品数量。 在过去的五天里,我一直在寻找答案,但在我得到的所有答案中,都有一些遗漏,导致应用程序出现错误或什么都不做。 我期待着您就如何最好地解决这一挑战提出建议。
提前谢谢。

您可以将侦听器添加到edittext中,该文本从用户处获取产品数量。默认情况下,它应为空或禁用,当用户选择该产品时,其文本应更改为1(根据我的逻辑),如果用户自己更改其中的值,则计算总数量

现在要获得该事件,请在EditText上添加TextWatcher

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
        }
    });

每当用户更改文本时,您都可以计算更新的总金额。

1。
OnCheckedChangeListener
添加到您的
复选框中,当用户首次
单击
复选框上的
时,显示单个
数量
总价
并在用户
取消选中
复选框时清除
数量
总价

2.
TextChangedListener
添加到
EditText
,以在用户开始键入
数量时显示总价

试试这个:

    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) {

        }
    });
FYI,在您的
price
TextView上,尝试只设置价格值(
1000
),而不是字符串(
1000/=
),因为我们从
price
TextView获取价格


希望这会有所帮助~

谢谢,让我试试。当然,是用户输入了价格,该值只是一个占位符,让我试试,谢谢这似乎会起作用,但我们必须处理它在“checkBoxChoice.setOnCheckedChangeListener…”中抛出的空指针异常。不知何故,应用程序无法在视图中找到复选框的确切位置。根据我到目前为止的研究,我尝试将该代码包装在与列表视图关联的点击侦听器上。我最终的结果是,没有错误,但在我点击“总文本”视图后,应用程序什么也不做。我们仍然缺少一些东西来让它工作。主listview布局的名称为“activity_sales2”,单个项目的布局名称为“sales_list2”。我希望他们能帮助设置正确的代码来获取这些视图。产品是从数据库填充到列表视图中的,这意味着可能会有50多种产品,因此需要在列表视图中正确位置的产品上设置一个监听器,其中有许多产品。