Click here to Skip to main content
Sign Up to vote bad
good
See more: Android
Hey, everyone.
I'm trying to make a basic tycoon game for Android
and I'm trying to increment the value of text-views every 5 seconds with a timer,
But the textview doesn't update.
Here's my code so far:
public class Town extends Activity implements OnClickListener {
	Timer timer;
	TimerTask task;
	TextView goldTV;
	TextView woodTV;
	TextView foodTV;
	TextView stoneTV;
	TextView cashTV;
	int gold = 20;
	int wood = 20;
	int food = 20;
	int stone = 20;
	int cash = 200;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_town);
		goldTV = (TextView) findViewById(R.id.textView1);
		woodTV = (TextView) findViewById(R.id.TextView01);
		foodTV = (TextView) findViewById(R.id.TextView02);
		stoneTV = (TextView) findViewById(R.id.TextView03);
		cashTV = (TextView) findViewById(R.id.TextView04);
		timer = new Timer();
		task = new TimerTask() {
			
			@Override
			public void run() {
				gold++;
				goldTV.setText(gold);
				try {
					this.wait(2000);
				}
				catch (InterruptedException e){
				}
			}
			
		};
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
}
Posted 27 Nov '12 - 12:00
Dots12328

Comments
AndroidVivek - 8 Dec '12 - 2:12
use handler , thread is good but for android handler is best and if you are making more process in background then use asynch task

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 435
1 OriginalGriff 315
2 Arun Vasu 293
3 CPallini 213
4 Zoltán Zörgő 194
0 Sergey Alexandrovich Kryukov 10,005
1 OriginalGriff 7,654
2 CPallini 4,171
3 Rohan Leuva 3,447
4 Maciej Los 2,974


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid