2

My program gives the user an instruction and then they have n seconds to respond. However when it asks you to Do Nothing the text view doesn't update at all, the other instructions pop up fine and they're all coded the same.

    public class SimpleActivity extends Activity implements SimpleGestureListener {

    String str = "";
    String strDirection = "";
    Random ranDirection = new Random();
    int intDirection;

    int scoreCounter;
    long timeStart;
    long timeEnd;

    TextView score;
    TextView allerts;
    TextView correct;

    private SimpleGestureFilter detector;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gesture);
        detector = new SimpleGestureFilter(this, this);

        scoreCounter = 0;
        catchValues();

    }

    public boolean dispatchTouchEvent(MotionEvent me) {
        this.detector.onTouchEvent(me);
        return super.dispatchTouchEvent(me);
    }

    public void catchValues() {

        /** Linked to gesture.xml */

        score = (TextView) findViewById(R.id.tvScore);
        allerts = (TextView) findViewById(R.id.tvAllerts);
        correct = (TextView) findViewById(R.id.tvCorrect);

        initiateRandom();

    }

    public void initiateRandom() {

        /** Generate random move */

        /** Log start time of move */

        long start = System.currentTimeMillis();
        timeStart = start;

        intDirection = ranDirection.nextInt(5);

        if (intDirection == 0) {
            allerts.setText("Please Swipe Up!");
            randomColour();
        } else if (intDirection == 1) {
            allerts.setText("Please Swipe Down!");
            randomColour();
        } else if (intDirection == 2) {
            allerts.setText("Please Swipe Left!");
            randomColour();
        } else if (intDirection == 3) {
            allerts.setText("Please Swipe Right!");
            randomColour();
        } else if (intDirection == 4) {
            allerts.setText("Do Nothing!");
            randomColour();

            doNothing();

        }

    }

    public void onSwipe(int direction) {

        /** Detect swipe and store result */

        switch (direction) {

        case SimpleGestureFilter.SWIPE_RIGHT:
            strDirection = "right";

            break;
        case SimpleGestureFilter.SWIPE_LEFT:
            strDirection = "left";

            break;
        case SimpleGestureFilter.SWIPE_DOWN:
            strDirection = "down";

            break;
        case SimpleGestureFilter.SWIPE_UP:
            strDirection = "up";


            break;
        }

        checkSwipe();

    }

    public void checkSwipe() {

        /** Compare time from instruction to action */

        long end = System.currentTimeMillis();
        timeEnd = end;

        /** Verify the action was correct */

        if (timeEnd < timeStart + 2000) {

            if (intDirection == 0) {
                if (strDirection.contentEquals("up")) {
                    correctSwipe();
                } else {
                    wrongSwipe();
                }

            } else if (intDirection == 1) {
                if (strDirection.contentEquals("down")) {
                    correctSwipe();
                } else {
                    wrongSwipe();
                }
            } else if (intDirection == 2) {
                if (strDirection.contentEquals("left")) {
                    correctSwipe();
                } else {
                    wrongSwipe();
                }
            } else if (intDirection == 3) {
                if (strDirection.contentEquals("right")) {
                    correctSwipe();
                } else {
                    wrongSwipe();
                }

            }

        } else {

            /** Incorrect action, reset score and start over */

            scoreCounter = 0;
            score.setText("Score:" + scoreCounter);
            strDirection = "";
            correct.setText("Too Slow!");
            initiateRandom();

        }

    }

    @Override
    public void onDoubleTap() {
        // TODO Auto-generated method stub

    }

    public void correctSwipe() {
        scoreCounter = scoreCounter + 100;
        score.setText("Score:" + scoreCounter);
        correct.setText("Correct!");
        strDirection = "";
        initiateRandom();

    }

    public void wrongSwipe() {
        correct.setText("Wrong!");
        scoreCounter = 0;
        strDirection = "";
        score.setText("Score:" + scoreCounter);

    }

    public void randomColour() {

        Random txtColor = new Random();

        allerts.setTextColor(Color.rgb(txtColor.nextInt(255),
                txtColor.nextInt(255), txtColor.nextInt(255)));
    }

    public void doNothing() {

        /** Does nothing for n seconds */

        long end = System.currentTimeMillis();
        timeEnd = end;

        while (timeEnd < timeStart + 2000) {

            timeEnd = System.currentTimeMillis();
        }

        if (strDirection.contentEquals("up")
                || strDirection.contentEquals("down")
                || strDirection.contentEquals("left")
                || strDirection.contentEquals("right")) {
            wrongSwipe();
        } else {
            correctSwipe();
        }

    }

}
0

1 Answer 1

2

Try using runOnUiThread(Runnable r) when you want to update your UI from a Non-UI Thread:

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

Something like:

 Activity_Name.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // here you put updates to the UI

        }
    });

Alternatively use onProgressUpdate() method of AsyncTask.

Reference:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.