每两分钟刷新一次listview

时间:2014-04-07 06:57:46

标签: android listview

我试图每两秒刷新一次ListView。执行第一个loadingTask,并且适配器从arraylist聊天填充ListView。然后每隔两秒钟调用refreshAdapterLoadingTask并作为连续循环执行并再次填充arraylist聊天。我需要从ArrayList中删除之前的项目并仅保留新项目。请问如何实现道路功能的最后效果。目前,我的数组不断将以前的httpcall中的项目填充为循环。

int refreshInterval =2000; //set refresh interval

class loadingTask extends AsyncTask<Void, Void,Void> {

//arraylist
    chat = new ArrayList<Chat>();


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();



    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        ...

                for (int i=0;i<room_length;i++){
                JSONObject obj_value = responseArray.getJSONObject(i);

                if (obj_value.has("@id"))
                    id=obj_value.getString("@id");

                if (obj_value.has("@version"))
                    version=obj_value.getString("@version");

        ...


            chatAdapter = new ChatAdapter(m,chat);
            msgList.setAdapter(chatAdapter); //set data to listview
            chatAdapter.notifyDataSetChanged();//update changes
            msgList.setSelection(msgList.getAdapter().getCount()-1);


            handler.postDelayed(refreshView, refreshInterval);


    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub



        return null;
    }




        public static class refreshAdapterLoadingTask extends AsyncTask<Void, Void,Void> {

    private String isMarketingMessage;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        //if(chat!=null)
            //chat.clear();
    }

    @Override
    protected void onPostExecute(Void result) { 
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        try {
            ...

            for (int i=0;i< responseArray.length();i++){
                JSONObject obj_value = responseArray.getJSONObject(i);

                if (obj_value.has("@id"))
                    id=obj_value.getString("@id");

                if (obj_value.has("@version"))
                    version=obj_value.getString("@version");

                if (obj_value.has("@content"))
                    content=obj_value.getString("@content");

                if (obj_value.has("@sentDate"))
                    sentDate=obj_value.getString("@sentDate");

                if (obj_value.has("player"))
                    player=obj_value.getString("player");

                chat.add(new Chat(isMarketingMessage, id, version,content, sentDate, player));


            }


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        //remove previous arraylist values
    //  for (int i=0;i<previousArrayListSize;i++){
        //  chat.remove(i);
        //}



        handler.postDelayed(refreshView, refreshInterval);

        chatAdapter = new ChatAdapter(m,chat);
        chatAdapter.notifyDataSetChanged();


    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        return null;
    }
}



}

更新

public static void process() {

    new Thread(new Runnable() {

        public void run() {
            try {
                // LiveScoredata();
                new refreshAdapterLoadingTask().execute();

            } catch (Exception e) {

            }

            m.runOnUiThread(new Runnable() {

                public void run() {
                    try {

                        if (chat!=null)
                            chat.clear();

                        //your list fill hear 
                        chatAdapter.notifyDataSetChanged();

                    } catch (Exception e) {

                    }
                }
            });
        }

    }).start();

}

2 个答案:

答案 0 :(得分:3)

// Using Timer and Runnable method you can list refresh

Timer  t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {
                   // your process 
        process();
    }

}, 0, 2000);

// Process method
private void process() {

    new Thread(new Runnable() {

        public void run() {
            try {
                // LiveScoredata();
            } catch (Exception e) {

            }
            runOnUiThread(new Runnable() {

                public void run() {
                    try {
                        //your list fill hear 
                        adapter.notifyDataSetChanged();

                    } catch (Exception e) {

                    }
                }
            });
        }

    }).start();

}

答案 1 :(得分:1)

尝试以下代码

new Handler().postDelayed(new Runnable() {
        @Override
        //automatically disappear after given time and start game
        public void run() {
            //call your loadingTask here
        }
    }, refreshInterval);

在后执行中调用相同的方法

希望这会对你有所帮助