Reputation: 39
I am trying to build a short memory game and I want a list of words to be shown.
At the start of the activity, the first word, then every second the next word and after the last word is shown the activity changes without user proc. I have tried some things but the closest I've been is just the last word being shown. Any thoughts?
Java file:
package com.example.pc.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class Game1Round1 extends Activity {
String[] round1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game1round1);
round1 = getResources().getStringArray(R.array.round1);
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateTextView();
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
Proceed();
}
private void updateTextView() {
int i= 0;
while ( i <13) {
TextView textView = (TextView) findViewById(R.id.randomTextView);
textView.setText(round1[i]);
i = i + 1;
}
}
public void Proceed() {
Intent intent1 = new Intent(this, Game1Start.class);
startActivity(intent1);
}
}
And my strings.xml:
<resources>
<string name="app_name">My Application</string>
<string-array name="round1">
<item>Cinnamon</item>
<item>Apple</item>
<item>Milk</item>
<item>Detergent</item>
<item>Cheese</item>
<item>Shampoo</item>
<item>Butter</item>
<item>Tangerine</item>
<item>Coffee</item>
<item>Marmalade</item>
<item>Cabbage</item>
<item>Meat</item>
<item>Sugar</item>
</string-array>
Upvotes: 0
Views: 82
Reputation: 4867
delete the updateTextView()
method and change your Thread
like this
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted() && i < 12) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView)findViewById(R.id.randomTextView)).setText(round1[i]);
i++;
}
});
}
Proceed();
} catch (InterruptedException e) {
}
}
};
t.start();
Upvotes: 1
Reputation: 1601
You can use this:
textView.postDelayed(new Runnable() {
@Override
public void run() {
lastCommentText.setText(...);
}
}, 1000);
Upvotes: 0
Reputation: 3914
In your run()
method after try
, catch
use finally
and put proceed()
method in that like below
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateTextView();
}
});
}
} catch (InterruptedException e) {
}
finally{ Proceed(); }
}
};
then you should start the thread
t.start();
Hope this solves your issue
Upvotes: 0