Question

I have 6 string to show in a textview in order. But some of the strings are empty and I want my program to detect empty strings, wait 1 sec for each empty string detection and then continue to next text. I tried to do it with using Handler and while codes but I failed, any help would be appreciated.

TextView Textview1; 
int str[] = {R.string.str1,R.string.str2,R.string.str3,R.string.str4,R.string.str5,R.string.str6};
int CurrentText = 0;    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Textview1 = (TextView)findViewById(R.id.textview1);
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Textview1.setText(str[CurrentText]);
            String crnt = (String) getText(str[CurrentText]);
            CurrentText++;              

            if(crnt.equals("empty")){                   
                Handler loop = new Handler();
                loop.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub                          
                        String crnt2 = (String) getText(str[CurrentText]);
                        while(crnt2.equals("empty")){                               
                            CurrentText++;
                            Textview1.setText(str[CurrentText]);
                            crnt2 = (String) getText(str[CurrentText]);                             
                        }                           
                    }
                }, 1000);
            }
Was it helpful?

Solution

i've solved

<<resources>
<string name="app_name">gdfg</string>
<string name="action_settings">Settings</string>
<string name="str1">Alice</string>
<string name="str2">empty</string>
<string name="str3">empty</string>
<string name="str4">empty</string>
<string name="str5">Jane</string>
<string name="str6">Beth</string>

>

public class MainActivity extends Activity {
TextView Textview1;
TextView Textview2;
int str[] = {R.string.str1,R.string.str2,R.string.str3,R.string.str4,R.string.str5,R.string.str6};
int CurrentText = 0;        
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Textview1 = (TextView)findViewById(R.id.textview1);
    Textview2 = (TextView)findViewById(R.id.textView2);
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Textview1.setText(str[CurrentText]);                
            String cntrl = (String) getText(str[CurrentText]);
            if(cntrl.equals("empty")){                  
                handlerTimer.postDelayed(taskUpdateStuffOnDialog, 1000);    
            }else{
                CurrentText++;  
            }
        }
    });
}

//Handler definiton...
private Handler handlerTimer = new Handler();   

private Runnable taskUpdateStuffOnDialog = new Runnable() { 
         public void run() {                    
             CurrentText++;              
             String cre = (String) getText(str[CurrentText]);   
             Textview1.setText(cre);    
                    if(cre.equals("empty")){
                           handlerTimer.postDelayed(this, 1000);                            
                    }else{
                        //Çalışmayı durdurmak için de aşağıdaki kod kullanılabilir: 
                        CurrentText++;
                        handlerTimer.removeCallbacks(taskUpdateStuffOnDialog);
                    }
         }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top