Miguel87
Miguel87

Reputation: 3

How can I change Text of Textview from other funcion?

this is my first question so I hope to make it clear.

I have one textView with some numerical text and next to it one button with one click listener and what I want is that when you click on the button the numerical value (>=0) of the TextView decrements in one. Here is part of my code:

    TextView Counter = new TextView(this);      
    if (intSeries != 0)
        Counter.setText(Integer.toString(intSeries));
    else
        Counter.setText("0");
    Counter.setId(4);
    tablaContador.addView(Counter,Tr);

    Button Done = new Button(this);
    Done.setText("-1");
    if (intSeries != 0)
        Done.setVisibility(View.VISIBLE);
    else
        Done.setVisibility(View.GONE);
    Done.setId(6);
    Done.setOnClickListener(this);

And this is the onClick funcion (part of it):

@Override
public void onClick(final View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
      case 6:{
            TextView text = (TextView)findViewById(4);
            int series = Integer.parseInt(text.getText().toString());
            series--;
            text.setText(series);
            if (series==0){
                Button boton = (Button)findViewById(6);
                boton.setVisibility(View.GONE);
            }
       }
    }

}

The error is when I try to make the setText inside the onClick function, I hope it can be fixed or maybe recieve other idea to do it. Thank you so much.

Upvotes: 0

Views: 91

Answers (4)

Tom Tsagkatos
Tom Tsagkatos

Reputation: 1494

I tried this and it worked:

//Create onClickListener
OnClickListener pickChoice = new OnClickListener()
{
    public void onClick(View v)
    {
        TextView txt = (TextView) findViewById(4);
        int number = Integer.valueOf(txt.getText().toString());
        txt.setText(String.valueOf(number -1));
    }
};


//Create layout
LinearLayout lnLayout = new LinearLayout(this);
lnLayout.setOrientation(LinearLayout.VERTICAL);

TextView txt = new TextView(this);
txt.setId(4);
txt.setText("0");
lnLayout.addView(txt);

Button Done = new Button(this);
Done.setText("-1");
Done.setId(6);
Done.setOnClickListener(pickChoice);
lnLayout.addView(Done);

setContentView(lnLayout);

Where are you creating your button inside? an activity? the part where you pass the onClickListener to the button doesn't make sense, maybe the button is getting a wrong listener and gets you an error every time you press the button ?

The code should be easy to understand, if there is anything you need me to explain please ask :)

Upvotes: 0

MrSangrief
MrSangrief

Reputation: 176

Variablenames in java can't start with a capital letter. That is reserved for classnames.

Counter -> counter

Done -> done

Upvotes: 0

Kovács Imre
Kovács Imre

Reputation: 715

You should setup series as an integer. And increase/descrease it as you wish. When you want to change the button's text convert the int to String.

Instead of:

text.setText(series);

use:

text.setText(String.valueOf(series));

Upvotes: 0

Nibor
Nibor

Reputation: 1116

I would avoid all this hardcoding of Ids, use resources instead.

Your call to

text.setText(series)

is passing an int. The only valid setText(int resId) overload expects a resource associated with the int value, i.e. a string resource.

Convert your series value to a string.

Something like:

text.setText(Integer.toString(series));

Upvotes: 2

Related Questions