android - 删除字符串中的字符和arrayList无法正常工作

时间:2016-10-25 05:09:42

标签: java android arraylist

我们假设我的arrayList长度为1,其值为2arrayList.get(0)将返回2)。

使用以下方法,我尝试从2中删除arrayList,从而使其长度为0.该方法还会删除textViewCurrExp中的最后一个值。

我遇到的问题是它并没有真正删除arrayList中的值,因为当执行另一个方法(显示arrayList的内容)时,2没有'已从arrayList中删除。

以下是方法:

public void backspace (View view) {
    TextView textViewCalcCurrExp = (TextView)findViewById(R.id.textViewCalcCurrExp);

    try {
        if (arrayList.size() > 0 ) {
            // removing the last item from the ArrayList
            arrayList.remove(arrayList.size() - 1);
        }

        String string = textViewCalcCurrExp.getText().toString();
        if(string.length() > 0) {
            // removing the last character from the TextView
            textViewCalcCurrExp.setText(string.substring(0, string.length() - 1));
        }
    }
    catch (Exception e) {
        TextView textViewCalcCurrRes = (TextView)findViewById(R.id.textViewError);
        textViewCalcCurrRes.setText("ERROR");
    }
}

所以,我的问题是,我可以在这个方法中做些什么改变,以便它做我想做的事情?

编辑:这是另一种方法(不相关的代码替换为//calculations happen here):

public void onClickEquals (View view) {

    TextView textViewError = (TextView) findViewById(R.id.textViewError);
    TextView textViewCalcPrevRes = (TextView) findViewById(R.id.textViewCalcPrevRes);
    TextView textViewCalcCurrExp = (TextView) findViewById(R.id.textViewCalcCurrExp);
    TextView textViewCalcPrevExp = (TextView) findViewById(R.id.textViewCalcPrevExp);
    double calc = 0;
    String calcOutputStr;
    String tempString;
    int c = arrayList.size();
    boolean boolCalcPerformed = false;

    try {
        //calculations happen here (with the value(s) of arrayList, 
        //in this case, arrayList.get(0) == 2 when arrayList.get(0) 
        //should be null after running method above)
        //For context, if arrayList held {2,+,3}, calcOutputStr = "5"
        }
        //HERE IS WHERE the values output to textViews (in this hypothetical case 
        //calcOutputStr = "2"; because arrayList.get(0) is holding the value 2,
        //when it should be calcOutputStr = "";)
            textViewCalcPrevExp.setText(textViewCalcCurrExp.getText()); //copy text from textViewCalcCurrExp to textViewCalcPrevExp
            textViewCalcCurrExp.setText(""); //remove text from textViewCalcCurrExp
            textViewCalcPrevRes.setText(calcOutputStr); //display calc
    }
    catch (Exception e) {
        e.printStackTrace();
        textViewCalcPrevExp.setText(textViewCalcCurrExp.getText());
        textViewCalcCurrExp.setText("");
        stringInput="";
        stringInputWithOp="";
        arrayList.clear();
        textViewError.setText("ERROR");
    }
}

1 个答案:

答案 0 :(得分:1)

public void backspace (View view) {
    TextView textViewCalcCurrExp = (TextView)findViewById(R.id.textViewCalcCurrExp);

    try {
        if (arrayList.size() > 0 ) {
            // it will remove the 2 on index 0 because your 2 is no zero index                           
            arrayList.remove(0);
        }

        String string = textViewCalcCurrExp.getText().toString();//its coming empty
        if(string.length() > 0) {
            // removing the last character from the TextView
            textViewCalcCurrExp.setText(string.substring(0, string.length() - 1));
        }else{
             // if array list is zero than set textview as "";
           textViewCalcCurrExp.setText(string.substring("");

           }


    }
    catch (Exception e) {
        TextView textViewCalcCurrRes = (TextView)findViewById(R.id.textViewError);
        textViewCalcCurrRes.setText("ERROR");
    }
}

你不明白你的文字观点没有任何价值,所以

if(string.length() > 0) {
            // removing the last character from the TextView
            textViewCalcCurrExp.setText(string.substring(0, string.length() - 1));
        }

这种情况变得虚假;

相关问题