如何在Handler中使用Timer?

时间:2014-02-22 12:27:13

标签: android

我想每10秒刷新一个适配器。我在ListView内加载Adapter Handler,我该怎么办?我如何使用Timer?我希望每当值发生变化时,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);

1 个答案:

答案 0 :(得分:0)

    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);