Update TextView with each string in String-array e

2019-08-16 00:27发布

I have a TextView and I would like to update the TextView every 5 second with each string in my String Array.

Here is the code I tried. It always shows only the last String in the String array.

TextView display;
EditText caption;
Thread thread;
String blinks;
String[] wc;
private CountDownTimer timer;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    display = (TextView) findViewById(R.id.display);
    caption = (EditText) findViewById(R.id.caption);

    timer = new CountDownTimer(5000, 20) {

        @Override
        public void onTick(long millisUntilFinished) {

            String[] wc = {"The","Qucik", "Brown","fox","Jumped"};
            for (int j = 0; j < wc.length; j++) {

                blinks = wc[j];
                final String[] titles = {"" + blinks + ""};

                for (int i = 0; i < titles.length; i++) {
                    display.setText(titles[i]);
                }

            }


        }

        @Override
        public void onFinish() {
            try{
                yourMethod();
            }catch(Exception e){

            }
        }
    }.start();
}

4条回答
Fickle 薄情
2楼-- · 2019-08-16 00:35
  • Problem: The last iteration of the for-loop (on j) sets the text to the last string.

You need to make sure that you call TextView.setText() only once per tick. Then you can go with your approach (CountDownTimer) or with Antrrommet's (Handler). With your approach, this will look like something like:

int counter = 0; // field member, NOT local variable

@Override
public void onTick(long millisUntilFinished) {
    display.setText("{" + wc[counter++] + "}");
    if (counter == 5) counter = 0;  
}
查看更多
太酷不给撩
3楼-- · 2019-08-16 00:41
final String[] wc = {"The", "Qucik", "Brown", "fox", "Jumped"};
        final android.os.Handler handler = new android.os.Handler();
        handler.post(new Runnable() {

            int i = 0;

            @Override
            public void run() {
                display.setText(wc[i]);
                i++;
                if (i == wc.length) {
                    handler.removeCallbacks(this);
                } else {
                    //5 sec
                    handler.postDelayed(this, 1000 * 5);
                }
            }
        });
查看更多
▲ chillily
4楼-- · 2019-08-16 00:53

try to play Around with handler in android , i think you are asing something like this :

String[] wc = {"The","Qucik", "Brown","fox","Jumped"};
int j = 0;
Handler handler = new Handler(); 
Runnable runit = new Runnable() { 

    @Override 
    public void run() { 
     try{

            for (; j < wc.length; j++) {

                blinks = wc[j];
                final String[] titles = {"" + blinks + ""};

                for (int i = 0; i < titles.length; i++) {
                    display.setText(titles[i]);
                }
                handler.postDelayed(this,5000);
            }
        }
        catch (Exception e) {
            //  handle exception
        }
    } 
}; 
handler.postDelayed(runit, 5000);
查看更多
家丑人穷心不美
5楼-- · 2019-08-16 01:00

If the answer is useful. Please do vote :)

        final String[] gritArray = getResources().getStringArray(R.array.gritInformation);
    //creating new handler allows send and process message
        final Handler gritInfoHanlder = new Handler();
    //adding runnable to message queque
            gritInfoHanlder.post(new Runnable() {
    //initializing array position
                int tipPosition = 0;

                @Override
                public void run() {
                    //set number of tip(randon/another way)
//setting array to textview
                    gritInfo.setText(gritArray[tipPosition]);
                    tipPosition++;
//if array exist its length remove callback
                    if (tipPosition == gritArray.length) {
                        gritInfoHanlder.removeCallbacks(this);
//delayed handler 10 sec
                    } else {
                        gritInfoHanlder.postDelayed(this, 1000 * 10);
                    }
                }
            });
查看更多
登录 后发表回答