2014-02-14 47 views
0

如何創建操作歷史記錄?如何爲Android應用創建歷史記錄?

我需要將結果保存在數組中,然後在Android中使用ListActivity重新存儲結果。

這是代碼:

public void calculator(){ 
    EditText screen = (EditText) findViewById(R.id.screen); 

    if (screen.getText().toString().equals(".")){ 
     screen.setText("0"); 
    } 
    if (screen.getText().toString().length() > 0){ 
     this.Operand2 = Float.parseFloat(screen.getText().toString()); 
    } 
    if (this.Operator.equals("+")) { 
     this.Answer = this.Operand1 + this.Operand2; 
    } else if (this.Operator.equals("-")){ 
     this.Answer = this.Operand1 - this.Operand2;  
    } else if (this.Operator.equals("*")){ 
     this.Answer =this.Operand1 * this.Operand2; 
    } else if (this.Operator.equals("/")){ 
     this.Answer = this.Operand1/this.Operand2; 
    }else if (this.Operator.equals("^")){ 
     this.Answer = (float) Math.pow(this.Operand1, this.Operand2); 
    }else if (this.Operator.equals("%")){ 
     this.Answer = Operand1 % this.Operand2; 
    }else{ 
     this.Answer = Float.parseFloat(screen.getText().toString()); 
    } 

    screen.setText(this.Answer + ""); 

而這正是數組適配器:

la1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ARRAY_NAME); 

回答

0

與方法計算器類()添加字段:

private List<String> history = new LinkedList<String>(); 

然後,添加回答這個名單:

screen.setText(this.Answer + ""); 
list.add(this.Answer); 

現在列表包含操作(答案)的歷史。

0

不要讓計算器這樣。解析整個表達式而不是'char by char'。 (LALR可以完美地完成這項工作)

準確地使用什麼,bison,這個工具將代碼爲你。

相關問題