1

I'm trying to learn Android by creating a calculator app. I have buttons with Ids button0 to button9. On clicking the button, their value should be be added to textViewAbove but I can't seem to be able to convert an integer into a string. I feel that the Android studio is taking my toString() method to be of integer.toString() type.

Code:

int[] buttonId = {R.id.button0, R.id.button1, R.id.button2, R.id.button3, R.id.button4,
        R.id.button5, R.id.button6, R.id.button7, R.id.button8, R.id.button9};


Button[] bt = new Button[10];
for (int i = 0; i < 10; i++) {       //If this doesn't work then do it separately.
    final int I = i;
    bt[I] = (Button) findViewById(buttonId[I]);

    bt[I].setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {

                    textViewAbove.append(toString(I));
                    //Enter action methods here.
                }
            }
    );
}

Error:

Error:(111, 50) error: method toString in class Object cannot be applied to given types;
required: no arguments
found: int
reason: actual and formal argument lists differ in length

Note that the code is inside the onCreate method in the MainActivity class.

Vishal Khanna
  • 53
  • 1
  • 10

4 Answers4

5

You are attempting to invoke the method String toString() of the anonymous subclass of Button.OnClickListener. This method takes no parameters, so it is complaining when you try to give it an int parameter.

You should invoke some other toString method which does accept an int (or Integer) parameter, e.g. Integer.toString(int).

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

You should use Integer.toString(I);

Miguel Benitez
  • 2,322
  • 10
  • 22
1

The toString method, which is inherited from the Object class, does not take any arguments.

Instead, you can use implicit conversion like textViewAbove.append(""+I); Or more favorably the textViewAbove.append(String.valueOf(I));

Vucko
  • 7,371
  • 2
  • 27
  • 45
0

Replace your int I with

final String I = i +"";

and remove the toString() method and simply use

textViewAbove.append(I);
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107