简体   繁体   English

如何在Handler中使用Timer?

[英]How to use Timer inside Handler?

I want to refresh an adapter every 10 seconds. 我想每10秒钟刷新一次适配器。 I'm loading my ListView Adapter inside Handler , so what do I do? 我正在Handler加载ListView Adapter ,那该怎么办? How do I use Timer ? 如何使用Timer I want that whenever the value changes, the Adapter automatically refreshes ListView . 我希望每当值更改时, Adapter自动刷新ListView

      @SuppressLint("HandlerLeak")
Handler handle = new Handler() {
    public void handleMessage(android.os.Message msg) {
        super.handleMessage(msg);
        CommonObjects.hideProgress();

        if (msg.what == 1) {

            plotadapter = new PlotsAdapter(Plots.this, arrayplot);

            plotslist.setAdapter(plotadapter);

        }
        if (msg.what == 2) {
            Toast.makeText(Plots.this, "Server, Not Available!",
                    Toast.LENGTH_SHORT).show();
            finish();
        }
    }
};
@Override
protected void onRestart() {
    if(CommonObjects.getLogoutreject().equals("1") && CommonObjects.logout){
   //           CommonObjects.logout=false;
        finish();

    }
    super.onRestart();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.plots);
    plotslist = (ListView) findViewById(R.id.plotslist);
    back = (ImageView) findViewById(R.id.plotback);
    bottomlayout = (RelativeLayout) findViewById(R.id.bottom_layout);
    scroll_down = (ImageView) findViewById(R.id.down);
    scroll_up = (ImageView) findViewById(R.id.up);
    plotadapter = new PlotsAdapter(Plots.this, arrayplot);
    plotslist.setAdapter(plotadapter);
    final Handler handler = new Handler();
            handler.postDelayed( new Runnable() {
            @Override
                public void run() {
                        if(condition)
                       {
                            // your code to check
                               plotadapter.notifyDataSetChanged();

                       }
                    handler.postDelayed(this, 1000);
                }
            }, 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM