-2

ボタンのクリック後に計算が行われ、この TextView に保存されない限り、常に $0.00 を表示する次の TextView があります。

        <TextView
            android:id="@+id/tvTotalAmount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="$0.00"
            android:textStyle="bold"
            android:paddingTop="10dp"
            android:textColor="#00A21E"
            android:textSize="25dp"
            android:paddingLeft="30dp"
            android:layout_below="@id/tvTotal" />
            <EditText
                android:id="@+id/etToll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberDecimal" />
            <Button
                android:id="@+id/btnCalculate"
                android:background="@drawable/calcbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calculate"
                android:layout_gravity="right"
                android:textStyle="bold"
                android:textColor="#FFFFFF"
                android:shadowColor="#000000"
                android:shadowDx="1"
                android:shadowDy="1"
                android:shadowRadius="2"
                android:layout_alignParentRight="true" />

アプリを実行すると、$0.0 が表示されます (ドルとセントを扱っていて、両方の数字が 00 でない限り、小数点以下 2 桁を表示したい場合を除いて、これは機能します)。

私は使用SharedPreferencesしていますが、アプリ内のコードは次のとおりです。

onCreate()

float totalTollAmount = 0;
    tollAmount = (EditText) mFrame2.findViewById(R.id.etToll); //where the amount is entered
    tollAmount.setFilters(new InputFilter[] {new DecimalFilter(6, 2)});
    tvTotalAmountLabel = (TextView) mFrame2.findViewById(R.id.tvTotalAmount); //holding total amount
    if (Float.toString(prefs.getFloat("dblTollAmount", 0)) != "0.0") {
        tollAmount.setText(Float.toString(prefs.getFloat("dblTollAmount", 0)));
    }
    tvTotalAmountLabel.setText("$" + Float.toString(prefs.getFloat("totalTollAmount", 0))); //retrieves the total toll amount and displays

btnCalc.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        strTollAmount = tollAmount.getText().toString();
        strInfName = nameOfInf.getText().toString();
        String k = "";
        if (strTollAmount.length() != 0 && strInfName.length() != 0) {
            float dblTollAmount = Float.parseFloat(strTollAmount);
            submitInfo(dblTollAmount);
        }
        Toast.makeText(getActivity(), k, Toast.LENGTH_SHORT).show();
    }
});

    private void submitInfo(float toll) {
        y = "TEST";
        if (y != null) {
            editor.putString("someId", y);
            totalTollAmount = round(toll + prefs.getFloat("totalTollAmount", 0));
            tvTotalAmountLabel.setText("$" + Float.toString(totalTollAmount));
            }
              else {
            editor.putString("someId", "");
        }
        editor.putString("previous-trip", y);
        editor.putFloat("totalTollAmount", round(totalTollAmount));
        //editor.commit(); removed on 8/3
        if (nameOfInf != null && nameOfInf.length() > 0) { // added on 8/3...
            //Clear out this value from Enter Data tab ONLY if Calculate happens
        }
        if (tollAmount != null && tollAmount.length() > 0) {
            //Clear out this value from the Enter Data tab ONLY if Calculate happens
        }
        editor.commit(); //added on 8/3
    }

private float round(float amount){
    return Float.parseFloat(new DecimalFormat("###.##").format(amount));
}

onDestroy方法:

@Override
public void onDestroy(){
    super.onDestroy();
    Log.i("OnDestroy", "Logged");
    if(nameOfInf != null && nameOfInf.length() > 0) {
        editor.putString("strInfName", nameOfInf.getText().toString());
    }
    if(tollAmount != null && tollAmount.length() > 0) {
        editor.putFloat("dblTollAmount", Float.parseFloat(tollAmount.getText().toString()));
    }

    editor.commit();
}

想定されること: ボタンを押すたびにcalculate、アプリは EditText にあるものを取得etTollし、TextViewtvTotalAmountに既にある値に追加する必要があり、毎回そうし続けます。

TextView の元の値は $0.00 ですが、$0.0 しか表示されません。ドルとセントを扱うので、EditText と TextView の小数点以下 2 桁を表示したいと思います。

直面している問題を解決するにはどうすればよいですか?

完全なコードが必要な場合は、お知らせください。

4

1 に答える 1