2017-05-05 14 views
0

正式にコードを学習してからわずか3ヶ月ですが、興味深いのです。 現在、私はシンプルなデータベースサンプルコードからアンドロイドスタジオを使って簡単な販売管理アンドロイドアプリケーションを作成しようとしています。 は、私は次のことを可能にするために、右の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> 

(そのコードを短縮し、今でも最も重要な部分を表示するように、私はいくつかの研磨のコードを削除しました)。 製品の名前と価格は、Contract、Dbhelper、Provider、およびCursor Adapterのような通常のJavaファイルを使用して作成されたSQLiteデータベースによって生成されます。 ユーザーが別のアクティビティに製品名と価格を入力し、さらに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コードを設定して、これらの計算を有効にし、合計金額を表示することが課題です。 クリックリスナーを設定する必要がありますが、ユーザーが複数の場所、つまりボタンをクリックする必要があり、購入する製品の数量を設定する必要があるため、設定する正しい方法はわかりません。 私は最後の5日間、答えを探していましたが、私が得たすべての答えに、アプリケーションにエラーがあるか何もしない何らかの行方不明がありました。 私はその挑戦をどのようにして解決するのか、あなたのご提案をお待ちしております。 ありがとうございます。

答えて

0

リスナーをユーザーの製品量に応じて追加することができます。デフォルトでは空白または無効にする必要があります。ユーザーがその製品のテキストを1に変更する必要があります。その値を変更して合計金額を計算します。ユーザーはあなたが更新され、合計金額を計算することができ、テキストを変更するたびに、あなたの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 
     } 
    }); 

にTextWatcherを追加し、そのイベントを取得するには今すぐ

+0

ありがとう、私はそれを試してみましょう。 – Mika369

0

1.ときときに、ユーザーun-checkCheckBoxquantity & total価格をクリアするには、ユーザーCheckboxclick第一および単一quantityためtotal価格を表示するには、あなたのCheckBoxOnCheckedChangeListenerを追加します。

2。TextChangedListenerEditTextを追加すると、ユーザーがquantityと入力したときの総額が表示されます。我々はpriceのTextViewから価格を取得しているとして、(1000/=)文字列の代わりにFYI、あなたのpriceのTextViewのは、価格だけの価値(1000)を設定してみてください

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

     } 
    }); 

はこれを試してみてください。

希望がありますように〜

+0

もちろん、それは価格を入力するユーザーです、その値は単に場所の所有者です、私はこれを試してみましょう – Mika369

+0

これは動作するようですが、私たちはNull Pointer Exceptionを処理しなければなりません " checkBoxChoice.setOnCheckedChangeListener ... "どういうわけか、アプリケーションはビュー上のチェックボックスの正確な位置を特定できません。今までの私の研究から、リストビューに結びつけられたオンのアイテムのクリックリスナの周りにそのコードをラップしようとしました。私はその状況ではエラーは発生しませんでしたが、アプリはテキストの全体表示をクリックしても何もしません。 – Mika369

+0

私たちはまだそれが機能するようにここで欠けているものがあります。メインのリストビューレイアウトの名前は "activity_sales2"で、個々のアイテムのレイアウト名は "sales_list2"です。私は、彼らがそれらの見解を得るための正しいコードを設定するのを助けることを願っています。 – Mika369

関連する問題