Problem with TextView in android
I have a problem with my TextView. I have a PlayScreenActivity that displays information about consumables. at the top of the screen. then I go to the store screen and buy supplies. when i go back everyone else changes but 1 and it displays the new value, if i change my phone and orientation changes it is the only way to see the purchased items added. Does anyone have an idea why this is happening?
I set the screen orientation to portrait to see if it will work if it can't switch, but it doesn't 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) );
}
}
source to share
... it will display the new value if I change my phone and orientation changes
Android will destroy and re-create the activity when configuration changes occur at runtime. It makes sense that your new variables are drawn to the screen, because all the activity is recreated:
http://developer.android.com/guide/topics/resources/runtime-changes.html
If most of your TextView widgets are refreshed when the PlayScreenActivity resumes playing, your call to refreshDisplay () works (to some extent). Which widget doesn't redraw? I would also like to add some entries and see how LogCat suggested zapl for example.
EDIT
Sample code with some logging (I moved widgets to fields ...)
public class PlayscreenActivity extends Activity
{
Data data_;
String TAG = "PlayScreenActivity";
TextView lemonsLeftTextLabel;
TextView sugarLeftTextLabel;
TextView iceLeftLabel;
TextView cupsLeftTextLabel;
TextView totalCashTextLabel;
Button shopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Creating PlayScreen activity...");
setContentView(R.layout.main);
Log.d(TAG, "Assigning buttons...");
lemonsLeftTextLabel = (TextView) findViewById(R.id.lemonsLeftText);
sugarLeftTextLabel = (TextView) findViewById(R.id.sugarLeftText);
iceLeftLabel = (TextView) findViewById(R.id.iceLeftText);
cupsLeftTextLabel = (TextView) findViewById(R.id.cupsLeftText);
totalCashTextLabel = (TextView) findViewById(R.id.totalCashText);
shopButton = (Button) findViewById(R.id.btnshop);
lemonsLeftTextLabel.setText("Lemons: ");
sugarLeftTextLabel.setText("Sugar: ");
iceLeftLabel.setText("Ice: ");
cupsLeftTextLabel.setText("Cups: ");
shopButton.setOnClickListener(new SupplyShopListener());
Log.d(TAG, "onCreate() complete");
}
private class SupplyShopListener implements View.OnClickListener {
public void onClick(View v){
//Intent i = new Intent(v.getContext(), SupplyShopActivity.class);
//startActivity(i);
refreshDisplay();
}
}
@Override
public void onResume(){
Log.d(TAG, "onResume() called");
super.onResume();
data_ = getData();
Log.d(TAG, "Data fetched");
Log.d(TAG, "Contents of data = " +
"Lemons: " + Integer.toString(data_.lemons_) + " | " +
"Sugar: " + Integer.toString(data_.sugar_) + " | " +
"Ice: " + Integer.toString(data_.ice_) + " | " +
"Cups: " + Integer.toString(data_.cups_) + " | " +
"Cash: " + data_.cash_ + " | ");
refreshDisplay();
}
@Override
public void onPause(){
Log.d(TAG, "onPause() called");
super.onPause();
saveData();
refreshDisplay();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
Log.d(TAG, "onConfigurationChanged() called");
super.onConfigurationChanged(newConfig);
}
private Data getData() {
Log.d(TAG, "getData() called - restoring");
GameData player_data = new GameData(this);
player_data.open();
Log.d(TAG, "Pulling data from game string...");
Data game_state = new Data(player_data.getGameString());
player_data.close();
Log.d(TAG, "Contents of data = " +
"Lemons: " + Integer.toString(game_state.lemons_) + " | " +
"Sugar: " + Integer.toString(game_state.sugar_) + " | " +
"Ice: " + Integer.toString(game_state.ice_) + " | " +
"Cups: " + Integer.toString(game_state.cups_) + " | " +
"Cash: " + game_state.cash_ + " | ");
return game_state;
}
private void saveData() {
Log.d(TAG, "saveData() called - saving");
GameData player_data = new GameData(this);
player_data.open();
Log.d(TAG, "Serializing data into GameData object");
Log.d(TAG, "Contents of data = " +
"Lemons: " + Integer.toString(data_.lemons_) + " | " +
"Sugar: " + Integer.toString(data_.sugar_) + " | " +
"Ice: " + Integer.toString(data_.ice_) + " | " +
"Cups: " + Integer.toString(data_.cups_) + " | " +
"Cash: " + data_.cash_ + " | ");
player_data.setGameString(data_.SerializeGame());
player_data.close();
}
private void refreshDisplay() {
Log.d(TAG, "refreshDisplay() called - redrawing UI");
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
String totalCash = nf.format(data_.cash_);
lemonsLeftTextLabel.setText("Lemons: " + Integer.toString(data_.lemons_));
sugarLeftTextLabel.setText("Sugar: " + Integer.toString(data_.sugar_));
iceLeftLabel.setText("Ice: " + Integer.toString(data_.ice_));
cupsLeftTextLabel.setText("Cups: " + Integer.toString(data_.cups_));
totalCashTextLabel.setText("Cups: " + totalCash);
}
}
source to share