Reputation: 33
I'm fairly new to programming for android and I'm having trouble updating a TextView in my code. I am able to set it and it updates properly from onResume().
Later, I call
startActivityForIntent(my.android.activity,0)
I then use setText in this method:
onActivityResult(int requestCode, int resultCode, Intent returnIntent){
if (resultCode == Activity.RESULT_OK){
String myString= returnIntent.getString("mystr","empty");
myClass=myClass.interperetResults(myString); //this method returns correctly
leftText.setText(""+myClass.toString());
leftText.invalidate() //neither invalidate() or postInvalidate() changes result
}
};
and it appears updated inside the TextView class (I've used System.out to debug this), but it does not update on the screen.
From what I can tell, I just need to call TextView.invalidate() or TextView.postInvalidate(), neither of which are effective. Is there something I'm missing, or what? I've tried many of the different suggestions from other posts, to no avail here.
I also have a rightText object and if statements for when the returned string is null, but this is the section I'm having issues with. If you guys need any other parts of my code, please let me know. I'd be more than happy to help. Thanks again for your input.
EDIT: As requested, here is my xml layout. Though the problem isn't me not being able to see the view. When I call setText in onResume() it displays and changes properly. Anyway, here's my xml code. I don't feel like changing the variable names to generic things, so this is what it actually is. I'm working with a partner on the project, and he's the one who created the xml and layout, so I'm not entirely sure what everything here is. I hope you guys can make sense of it.
Here's onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
}
And onResume:
protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
setContentView(R.layout.play);
myEquation = new Equation(getResources());
leftText = (TextView) this.findViewById(R.id.playLeftEquation);
rightText = (TextView) this.findViewById(R.id.playRightEquation);
//leftText.setText("No data");
//rightText.setText("No data"); //The textviews update fine here.
//Currently commented out because I thought that maybe this was
//overwriting the other setText, but still no change.
}
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/atomic_bckgrd"
android:columnCount="11" >
<ImageButton
android:id="@+id/playTrashCanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="left"
android:layout_row="0"
android:contentDescription="@string/trash"
android:onClick="clickHandler"
android:src="@drawable/trash" />
<Button
android:id="@+id/playEditButton"
android:layout_column="2"
android:layout_row="0"
android:onClick="clickHandler"
android:text="@string/edit" />
<ImageButton
android:id="@+id/playRulesButton"
android:layout_column="4"
android:layout_columnSpan="5"
android:layout_row="0"
android:contentDescription="@string/rules"
android:onClick="clickHandler"
android:src="@drawable/rules1" />
<TextView //One of the non-updating fields
android:id="@+id/playLeftEquation"
android:layout_column="1"
android:layout_gravity="bottom"
android:layout_row="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="@+id/playEnterButton"
android:layout_column="2"
android:layout_gravity="bottom"
android:layout_row="1"
android:contentDescription="@string/enter"
android:onClick="clickHandler"
android:src="@drawable/enter1" />
<TextView //The other non-updating field
android:id="@+id/playRightEquation"
android:layout_column="3"
android:layout_columnSpan="6"
android:layout_gravity="bottom"
android:layout_row="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Space
android:layout_width="52dp"
android:layout_height="1dp"
android:layout_column="0"
android:layout_row="0" />
<Space
android:layout_width="148dp"
android:layout_height="1dp"
android:layout_column="1"
android:layout_row="0" />
<Space
android:layout_width="161dp"
android:layout_height="1dp"
android:layout_column="2"
android:layout_row="0" />
<Space
android:layout_width="46dp"
android:layout_height="1dp"
android:layout_column="3"
android:layout_row="0" />
<Space
android:layout_width="36dp"
android:layout_height="1dp"
android:layout_column="4"
android:layout_gravity="fill_horizontal"
android:layout_row="0" />
<Space
android:layout_width="1dp"
android:layout_height="160dp"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_row="0" />
</GridLayout>
Upvotes: 0
Views: 3648
Reputation: 33
FIXED: I have figured out the problem. After removing the toString method from myEquation to see the addresses used, I realized that I was initializing myEquation in onResume(). (I had actually moved it to onStart() by this point, but that had the same effect)
So yeah. Initialize all your variables in onCreate() and avoid massive headaches.
Upvotes: 1