开发者

Refresh views within a function

I'm trying perform some steps inside a function on Android. I would like to tell to user what is happen in a specific moment without exit of my function. something like it:

public boolean updateServiceList() {
        LinearLayout start = (LinearLayout) findViewById(R.id.start);
        LinearLayout major = (LinearLayout) findViewById(R.id.major);
        TextView messenger = (TextView) findViewById(R.id.messenger);
        Integer i = 0;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
         boolean isOnline = cm.getActiveNetworkInfo().isConnectedOrConnecting();
         if(isOnline==false) {
             Button exit = (Button) findViewById(R.id.exit_btn);
             ProgressBar pg = (ProgressBar) findViewById(R.id.progress);
                exit.setText("OK");
                exit.setVisibility(View.VISIBLE);
                exit.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        gMain.this.finish();
                    }
                });
                pg.setVisibility(View.GONE);
             messenger.setText(R.string.nointernet);


             while(isOnline==false) {
                                 i++;
                                 messenger.setText(messenger.getText()+"Try: "+ i.toString);
                                  boolean isOnline = cm.getActiveNetworkInfo().isConnectedOrConnecting();
                             }
             return false;

         } else {
             isOnline =  cm.getActiveNetworkInfo().isConnected();
             while (isOnline==false || 1==1) {
                 isOnline =  cm.getActiveN开发者_开发技巧etworkInfo().isConnected();

             }
    }

    return true;

} 

My problem is in "while(isOnline)" where I can't see the update messages. I tried invalidate() and postInvalidate() there but no results. Any ideas?

EDIT:

I found the solution! There it is: How to refresh a TextView while looping in Android?

The Rackers's answer:

public class myClass extends Activity{

private Handler mHandler;
private TextView text;
private int i;

@Override
  public void onCreate(Bundle savedInstanceState) {
      text = (TextView) findViewById(R.id.textView01);
      i = 0;
      mHandler = new Handler();
      mHandler.post(mUpdate);

  }

private Runnable mUpdate = new Runnable() { public void run() {

   text.setText("My number: " + i);
   i++;
   mHandler.postDelayed(this, 1000);

}

};}

So, at my case I just call the others steps if the current is solved. If not, I just post the step again...


Your approach will not work.

When you call messenger.setText(), you think the screen is supposed to update right then. You are mistaken.

What really happens when you call messenger.setText() is that a message goes on a message queue, to be processed by the main application thread. That's the same thread you're tying up with your while() loop. While you are in that while() loop, your entire UI is frozen: no clicks, no updates, nothing.

I suggest that you get rid of the while() loop and move along with the rest of your app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜