0

This code is too add a timer on the code.. but i am having a problem with my code when it comes to the timer code... How will i improve the code for the timer inside the for loop of my code.. Hopefully you understand the my code.. and have improved my code.. Thanks!!!!

package com.thesis.americansignlanguage;

import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class AlphabetCompareClass extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alphabetcompare);
    final String get;
    Bundle gotWord = getIntent().getExtras();
    get = gotWord.getString("key");
    TextView Word = (TextView)findViewById(R.id.textView1);
    final ImageView img = (ImageView)findViewById(R.id.image_alpha) ;
    Word.setText(get);

    for(int x = 0; x > gotWord.getString("key").length(); x++) {

        final InputStream is;
            Timer timer = new Timer();
            TimerTask timertask = new TimerTask() {

                @Override
                public void run() {
                    try {
                        is = getResources().getAssets().open(get + ".png");
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        img.setImageBitmap(bitmap);

                    } catch (IOException e) {
                        return;
                    }


                }
            };
        }


    };

}
kathleen55
  • 341
  • 2
  • 9
  • 29

2 Answers2

0

TimerTask runs on a different thread. Ui can be updated only from ui thread.

This img.setImageBitmap(bitmap); should be executed on the ui thread

Use runOnUiThread. You can also use a Handler. Also i do not understand the need for the for loop.

runOnUiThread(new Runnable() {
@Override
public void run() {
    img.setImageBitmap(bitmap);
}
});
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • i use the for loop for example when i input hello the for loop will be determined on how long the length of the inputted string. – kathleen55 Feb 24 '14 at 10:16
  • @Joseph i still don't understand the need. aside form that. i haven't mentioned the first mistake which is psoted in prosper k post. Secondly use a Handler. – Raghunandan Feb 24 '14 at 10:18
  • what first mistake sir? also sir this runOnUiThread is this will be added on the public void run()? – kathleen55 Feb 24 '14 at 10:29
  • @Joseph `timer.schedule(timertask, 500, 3000);` schedule a timer. yes have the runOnUiThread inside timertask run;s method. I still don't think you require a `for` loop – Raghunandan Feb 24 '14 at 10:29
  • but why sir? why don't i need a for loop? @Raghunandan – kathleen55 Feb 24 '14 at 10:47
  • I guess you want to repeat some task for which you can use a Handler or a timer task and you do not require a for loop. – Raghunandan Feb 24 '14 at 10:48
  • ohh but i dont know how to do that. and then im also a beginner in android programming so i have limited knowledge to this.. thats why im only trying to use for loop @Raghunandan – kathleen55 Feb 24 '14 at 10:50
  • @Joseph check the handler @ http://stackoverflow.com/questions/17839419/android-thread-for-a-timer – Raghunandan Feb 24 '14 at 10:51
  • @Ragunandan Sir where will i put the timer.schedule code? – kathleen55 Feb 24 '14 at 12:13
  • @Joseph you study the basics first. Read the docs. This is getting into a discussion. Place it where you have your timer object. You have solution. dig deep and solve your problem further – Raghunandan Feb 24 '14 at 12:15
0

Two problem found in current code:

First : Not using timer instance to schedule timerTask. do it as:

timer.schedule(timertask, 500, 3000);

Second : Updating UI from non- ui thread inside run method of TimerTask

Use runOnUiThread or Handler for updating Ui from run method of TimerTask

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • @Joseph : see @ Raghunandan edit answer also use `Your_Activity.this.runOnUiThread` instead of `runOnUiThread` because may be method not available inside Timertask context. – ρяσѕρєя K Feb 24 '14 at 10:26