I am having an issue with my TextView. I have a PlayScreenActivity that displays supplies info. on the top of screen. then I go to the store screen and buy supplies. when I come back all others change but 1 and it will display the new value if I turn my phone and orientation changes, thats the only way I can see the added supplies purchased. Anyone have an Idea why this is happening?
I set screen orientation to portrait to see if it will work if it can’t switch but doesnt fix.
EDIT
Here is a copy of my Activity Code:
public class PlayscreenActivity extends Activity {
Data data_;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((TextView)findViewById(R.id.lemonsLeftText)).setText(
"Lemons: " );
((TextView)findViewById(R.id.sugarLeftText)).setText(
"Sugar: " );
((TextView)findViewById(R.id.iceLeftText)).setText(
"Ice: " );
((TextView)findViewById(R.id.cupsLeftText)).setText(
"Cups: " );
((Button)findViewById(R.id.btnshop)).setOnClickListener(
new SupplyShopListener());
}
private class SupplyShopListener implements OnClickListener{
public void onClick(View v){
Intent i = new Intent(v.getContext(), SupplyShopActivity.class);
startActivity(i);
//refreshDisplay();
}
}
@Override
public void onResume(){
super.onResume();
data_ = getData();
refreshDisplay();
}
@Override
public void onPause(){
super.onPause();
saveData();
refreshDisplay();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
//refreshDisplay();
}
private Data getData() {
GameData player_data = new GameData(this);
player_data.open();
Data game_state = new Data(player_data.getGameString());
player_data.close();
return game_state;
}
private void saveData() {
GameData player_data = new GameData(this);
player_data.open();
player_data.setGameString(data_.SerializeGame());
player_data.close();
}
private void refreshDisplay() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
String totalCash = nf.format(data_.cash_);
((TextView)findViewById(R.id.lemonsLeftText)).setText(
"Lemons: " + Integer.toString(data_.lemons_) );
((TextView)findViewById(R.id.sugarLeftText)).setText(
"Sugar: " + Integer.toString(data_.sugar_) );
((TextView)findViewById(R.id.iceLeftText)).setText(
"Ice: " + Integer.toString(data_.ice_) );
((TextView)findViewById(R.id.cupsLeftText)).setText(
"Cups: " + Integer.toString(data_.cups_) );
((TextView)findViewById(R.id.totalCashText)).setText(
"Cash : " + (totalCash) );
}
}
Android will destroy and re-create the activity when run-time configuration changes occur. It makes sense that your new variables are being drawn on screen rotation, because the entire activity is re-created:
http://developer.android.com/guide/topics/resources/runtime-changes.html
If most of your TextView widgets are updating when your PlayScreenActivity is resumed, your refreshDisplay() call is working (to a degree). Which widget doesn’t redraw? I’d also throw in some logging and watch the LogCat like zapl suggested.
EDIT
Sample code with some logging (I moved the widgets into fields…)