ListView无法在ArrayAdapter&上正确刷新的AsyncTask

时间:2014-02-03 10:35:42

标签: android-listview android-asynctask refresh android-arrayadapter

我已经搜索了几乎所有的网站,以了解如何刷新列表视图,但不知何故,刷新仍然无法正常工作&我的应用程序在一段时间后崩溃了。 Functonality:我的应用程序从web(odata)服务器&正确地在listview中打印它。但是当我试图刷新它时:没有任何作用。请让我知道我做错了什么..感谢任何帮助.. 请在下面找到我的代码..

MainActivity.java:

public class MainActivity extends Activity {
    Button call;static boolean running=true;
    ListView list; ArrayList categories;
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //TODO Auto-generated method stub
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        call = (Button) findViewById (R.id.mybtn);
        list = (ListView) findViewById(R.id.mylistview);
        categories = new ArrayList();

        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
            // TODO Auto- generated method stub
            new callService().execute(); 
                }
        });
    }
    public class callService extends AsyncTask<Void, Void, ArrayList<String>> {
        @Override
        protected ArrayList<String> doInBackground(Void... params) {
       // TODO Auto-generated method stub
       if(running){
       ODataConsumer c =    ODataJerseyConsumer.create("http://x.x.x.x/CCTickerService/CCTickerService.svc/");      
       List<OEntity> listEntities = c.getEntities("LatestExchRates").filter("ExchangeID eq 21 and FutureYear eq 2006 and FutureMonth eq 7").execute().toList();
       if (listEntities.size() > 0) {
       for (OEntity entity : listEntities) {categories.add("\nExId-  "+entity.getProperty("ExchangeID").getValue().toString()+"   Last "+entity.getProperty("Last").getValue().toString()+"  Open-"+entity.getProperty("Open").getValue().toString());}
             }
       }
       return categories;
    }
    @Override
    protected void onPostExecute(ArrayList<String> result) {
        //  TODO Auto-generated method stub
        super.onPostExecute(result);
        adapter = new ArrayAdapter<String>(MainActivity.this,   android.R.layout.simple_list_item_1, result);
        list.setAdapter(adapter);
       //CODE TO REFRESH EVERY 25 SECOND    
       final Handler handler = new Handler();
       handler.postDelayed( new Runnable() {
   @TargetApi(Build.VERSION_CODES.HONEYCOMB)
   @Override
       public void run() {
            handler.postDelayed(this, 25*1000 );
            new callService().execute();
            categories.clear();
            adapter.addAll(categories);
            adapter.notifyDataSetChanged();
            list.invalidateViews();
                }
            }, 25*1000 );
        Toast.makeText(MainActivity.this, "refresh", Toast.LENGTH_SHORT).show();
        }
    }
}

我还尝试停止异步任务(通过clean(true))。但那也没有用。问题是:对于第一次2-3次,数据在25秒内更新,但后来执行变为v.fast&amp;值未正确显示,然后应用程序崩溃。如果我可以采取其他替代方案,请告诉我。

1 个答案:

答案 0 :(得分:0)

我找到了问题的解决方案。这是因为: “ handler.postDelayed(this,25 * 1000) ;;” 评论后,代码工作正常。

以上语句使代码再次进入循环。&amp;因此破坏了循环。:))