在同一TextView中显示字符串的arrray,时间间隔为5秒

时间:2015-12-14 07:42:25

标签: android arrays json

我正在使用android应用程序从url检索JSON数据并显示它。

我的要求: -  我有JSON数据数组,我必须每5秒钟显示一次数组项数据,一旦执行了所有数组项,它应该像往常一样再次调用onpostexecute它应该从第一个数组开始

  private class DownloadJSON extends AsyncTask<Void,Void,Void> {

        @Override
        protected Void doInBackground(Void... params) {

            arraylist = new  ArrayList<HashMap<String, String>>();
            JSONObject jsonobject = JSONfunctions.getJSONfromURL(url);

            try {

                String link = jsonobject.getString("link");
                jsonarray = jsonobject.getJSONArray("api");

                for (int i = 0; i < jsonarray.length(); i++) {

                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);

                    String title = jsonobject.getString("Title");
                    String subtitle = jsonobject.getString("Subtitle");
                    String image = jsonobject.getString("image");

                    map.put("title",jsonobject.getString("Title"));
                    map.put("sub",jsonobject.getString("Subtitle"));
                    map.put("image",link+jsonobject.getString("image"));

                    arraylist.add(map);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }


            return null;
        }


        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);


            for (int i=0; i<arraylist.size();i++){
                final String tit =   arraylist.get(i).get("title");
                final String subtit =   arraylist.get(i).get("sub");
                String img =   arraylist.get(i).get("image");

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                       while (true){
                           try{

                               Thread.sleep(5000);
                               mHandler.post(new Runnable() {
                                   @Override
                                   public void run() {
                                         t1.setText(tit);//t1,t2 is textview
                                         t2.setText(subtit);
                                   }
                               });

                           }catch (Exception e){

                           }
                       }

                    }
                }).start();

            }
        }
    }

实际上这段代码部分正常工作,这意味着第一次工作,但是5秒后它会刷新,但是数据在一段时间后数据发生变化后数据没有更新我不知道它是如何工作的,正如我所要求它应该执行的那样每5秒一次的每个项目,它应该在项目执行完成之后重新启动onpostexecute然后像往常一样重新启动等等

2 个答案:

答案 0 :(得分:2)

试试这个:

全局声明:

private TimerTask NoticeTimerTask;
  private final Handler handler = new Handler();
  Timer timer = new Timer();

从onPostExecute()

调用该函数
        setViews(arraylist);

setViews()

 private void setViews(final ArrayList<HashMap<String, String>> arraylist) {
        final int max = arraylist.size();
        final int[] current = {0};
        NoticeTimerTask = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        if(current[0] <max) {
                            Log.d("NoticeTimerTask", String.valueOf(current[0]));
                            t1.setText(arraylist.get(current[0]).get("title"));
                            t2.setText(arraylist.get(current[0]).get("sub"));
                            current[0]++;
                        }
                        else {
                            Log.d("NoticeTimerTask", String.valueOf(current[0]) + " timer.cancel();");
                           // current[0] = 0; // do this if yu want to loop back from first item
                            timer.cancel(); // do this if you want to stop
                        }
                    }
                });
            }
        };
        if (timer!=null) {
            timer.schedule(NoticeTimerTask, 0, 5000);
        }
    }

答案 1 :(得分:0)

你不需要为每个循环创建新的TimerTask,只需将定时器调度为5s,获取并更新TimerTask的run方法上的值

//declare index variable
private int index =0;
你在postExecute方法上的

//调用:

timer = new Timer();

timerTask = new TimerTask() {

        public void run() {



            //use a handler to run a toast that shows the current timestamp

            handler.post(new Runnable() {

                public void run() {
                final String tit =   arraylist.get(index).get("title");
                final String subtit =   arraylist.get(index).get("sub");
                String img =   arraylist.get(index).get("image");
                t1.setText(tit);//t1,t2 is textview
                t2.setText(subtit);
               index ++;

                }

            });

        }

    };
    timer.schedule(timerTask, 0, 5000); //