views:

191

answers:

3

Hi,

I´m working with a lineal layout and I´m trying to change the text of a TextView but it doesn´t refresh. When I debug I´ve checked that the text have changed correctly.

Thanks

public class Position extends Activity {
 Button botonempezar;
 Button botonsalir;
 TextView text;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.position);
        botonsalir = (Button) findViewById(R.id.salir);
        botonempezar = (Button) findViewById(R.id.Empezar);
        text = (TextView) findViewById(R.id.Textoposicion);
        botonsalir.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            onDestroy();
            finish();
           }
        });
        botonempezar.setOnClickListener(new View.OnClickListener() {   
              @Override
            public void onClick(View v) {
                 // TODO Auto-generated method stub
                 searchPosition();    
            }
        });
    }
    private void searchPosition(){
        boolean condition = true;
        do{
           determinatePosition();
        }while(condition == true);
    }

    private void determinatePosition(){
           ....
      this.text.setText("New text");
      //this.text.invalidate();
      this.text.postInvalidate();
      Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT);// this doesnt work neither.
         ....
    }

Here I post the code of the xml file. Its really silly but it could be the problem:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <TextView 
        android:layout_height="wrap_content" 
        android:id="@+id/Textoposicion" 
        android:text="Posición desconocida" 
        android:layout_width="fill_parent" 
        android:layout_marginTop="20dip" 
        android:inputType="text">
    </TextView>
    <Button android:layout_height="wrap_content" 
        android:id="@+id/Empezar" android:text="Empezar" 
        android:layout_width="fill_parent" 
        android:layout_marginTop="20dip">
    </Button>
    <Button 
        android:layout_height="wrap_content" 
        android:id="@+id/salir" 
        android:layout_marginTop="20dip" 
        android:text="Finalizar programa" 
        android:gravity="center" android:layout_width="fill_parent">
    </Button>


</LinearLayout>

I have edited the post because I omitted part of the code to make it simpler but I think I may suppressed the problem too. It runs in a endless loop, could be this the mistake?

+1  A: 

Hello,

Try to call invalidate on the view.

http://developer.android.com/reference/android/view/View.html#invalidate()

this.text.invalidate();

This will cause a redraw.

Sorry, too quick, I saw that you already tried that. Didn't that work? Why did you comment that away?

Rickard Lindroth
A: 

Hi,

Yes, I´ve tried that the first. I used this.text.setText("New text"); first but it didn´t work at all. Even Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT); doesn´t makes any message in the screen.

Thanks.

Miguel
A: 

Try not using getBaseContext(). Use Position.this

Well now your infinite loop is caused by


    private void searchPosition(){
        boolean condition = true;
        do{
           determinatePosition();
        }while(condition == true);
    }

Try moving the toast into public void onClick(View v)

Also, show us your imports

Falmarri
I´ve tried and it doesn´t work neither. Any other idea? It could be related with the position.xml file?
Miguel