我想在用戶將東西添加到購物車時更改TextView。初始值爲0.00,並且在用戶添加項目時,將其添加到值中。點擊允許用戶選擇項目的按鈕時,彈出AlertDialog對話框。在Android中更改TextView的值
我的問題是一個java.lang.StringToReal.invalidReal錯誤。我認爲我可能沒有正確理解TextVeiw的價值,但我不完全確定。
感謝任何人看着這個。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.pickItem);
builder.setItems(R.array.items, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
CartItems ci = new CartItems();
ci.setItem(which);
ci.setPrice(which);
cart.add(ci);
totalPriceTV = (TextView)findViewById(R.id.textView2);
double totalPrice = Double.parseDouble(totalPriceTV.toString());
totalPrice += ci.getPrice();
String newTotal = new Double(totalPrice).toString();
totalPriceTV.setText(newTotal);
}
});
builder.create();
builder.show();
}
});
}
發佈整個錯誤 – 2014-09-02 02:25:46
也許使用totalPriceTV.getText();而不是totalPriceTV.toString()? – Jerry 2014-09-02 02:29:01