如何在android中每分钟运行asyncTask并在UI上替换?

如何在android中每分钟运行asyncTask并在UI上替换?,android,Android,我想从本地服务器xammp中的JSON文件加载更新的数据。 我已经从json文件中获取了数据 这里是我的fragment_one.xml <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" an

我想从本地服务器xammp中的JSON文件加载更新的数据。 我已经从json文件中获取了数据

这里是我的fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusableInTouchMode="true"
        android:orientation="vertical">

        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="@null">
        </ListView>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:orientation="horizontal"
                android:layout_weight="1"
                android:layout_marginLeft="5dp">

                <TextView
                    android:id="@+id/txtbExtras"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textColor="#000000"
                    android:textSize="12dp"
                    android:text="Extras:"/>
                <TextView
                    android:id="@+id/txtbWide"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textColor="#000000"
                    android:textSize="12dp"
                    android:text="W-,"/>
                <TextView
                    android:id="@+id/txtbNb"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textColor="#000000"
                    android:textSize="12dp"
                    android:text="Nb-,"/>

                <TextView
                    android:id="@+id/txtbBye"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textSize="12dp"
                    android:textColor="#000000"
                    android:text="B-,"/>

                <TextView
                    android:id="@+id/txtbLegBye"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textSize="12dp"
                    android:textColor="#000000"
                    android:text="Lb-,"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_weight="2"
                android:layout_marginRight="5dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txtbTotal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:textSize="13dp"
                    android:textColor="#000000"
                    android:text="Total:"/>
                <TextView
                    android:id="@+id/txtbRR"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:textSize="13dp"
                    android:textColor="#000000"
                    android:text="RR:"/>
            </LinearLayout>
        </LinearLayout>
        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#80CBC4"></View>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:layout_marginLeft="5dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/txtyetBat"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="12dp"
                android:text="What is you rname"/>
        </LinearLayout>
        <ListView
            android:id="@+id/listBowling"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="@null">
        </ListView>

        <ListView
            android:id="@+id/listFallofWickets"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="@null">
        </ListView>
    </LinearLayout>
</ScrollView>

这里是我的OneFragment.java,它在布局上方膨胀并为我的适配器类设置值

package com.domore.tabfinaldemo;

import android.app.ProgressDialog;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import Adapter.FirstBowlingAdapter;
import Adapter.FullScoreAdapter;
import Adapter.WicketAdapter;


public class OneFragment extends Fragment {

    View rootView;

    ListView lst,lstBowling,lstWicket;
    FullScoreAdapter adapter=null;
    FirstBowlingAdapter adapter_bowlilng=null;
    WicketAdapter wicketAdapter=null;

    ArrayList<String> p_name=null;
    ArrayList<JSONObject> p_key=null;
    ArrayList<JSONObject> bowling_object=null;
    ArrayList<String> lstbowling=null;
    ArrayList<String> fall_wicket=null;
    ArrayList<String> player_key1=null;

    ArrayList<String> symmentric=null;
    ArrayList<String> union=null;
    ArrayList<String> intersection=null;
    ArrayList<String> p_name11=null;
    ArrayList<String> playing_11=null;


    View batting_header,bowling_header,wicket_header;

    TextView txtBattingtotal;
    String bat_order="";
    String[] parts;
    String[] score;

    Typeface tf;
    String jsonStr="";

    String target="";
    String wicket="";
    String[] inn_value;
    String topScore="";
    String inning_name="";
    String team_name="";
    String yetBat="";
    String strextras="",strwide="",strnb="",strbye="",strlb="",strtotal="",strRR="";

    JSONArray team_value;
    JSONObject teamobject;

    TextView txtextras;
    TextView txtwide;
    TextView txtNb;
    TextView txtBye;
    TextView txtLb;
    TextView txtTotal;
    TextView txtRR;
    TextView txtyetBat;

    private static final String url="http://10.0.2.2/JSON/fullList.php";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ScoreCard scoreCard=(ScoreCard)getActivity();

        bat_order=scoreCard.setInnings();
        topScore=scoreCard.setInningScore();

        rootView = inflater.inflate(R.layout.fragment_one, container, false);
        new JSONLoader().execute();

        txtextras=(TextView)rootView.findViewById(R.id.txtbExtras);
        txtwide=(TextView)rootView.findViewById(R.id.txtbWide);
        txtNb=(TextView)rootView.findViewById(R.id.txtbNb);
        txtBye=(TextView)rootView.findViewById(R.id.txtbBye);
        txtLb=(TextView)rootView.findViewById(R.id.txtbLegBye);
        txtTotal=(TextView)rootView.findViewById(R.id.txtbTotal);
        txtRR=(TextView)rootView.findViewById(R.id.txtbRR);
        txtyetBat=(TextView)rootView.findViewById(R.id.txtyetBat);

        tf=Typeface.createFromAsset(getActivity().getAssets(), "Roboto-Medium.ttf");
        lst=(ListView)rootView.findViewById(R.id.listview);

        batting_header=inflater.inflate(R.layout.batting_header, lst, false);

        txtBattingtotal=(TextView)batting_header.findViewById(R.id.txtBattingtotal);
        txtBattingtotal.setTypeface(tf);

        lstBowling=(ListView)rootView.findViewById(R.id.listBowling);
        bowling_header=inflater.inflate(R.layout.bowling_header,lstBowling,false);

        lstWicket=(ListView)rootView.findViewById(R.id.listFallofWickets);
        wicket_header=inflater.inflate(R.layout.wicket_header,lstWicket,false);

        return rootView;
    }

    public class JSONLoader extends AsyncTask<String, String, String> {

        private ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

//            dialog = new ProgressDialog(getActivity());
//            dialog.setMessage("Getting....");
//            dialog.setIndeterminate(false);
//            dialog.setCancelable(true);
//            dialog.show();
        }

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

            ServiceHandler sh = new ServiceHandler();
            jsonStr = sh.makeServiceCall(url, ServiceHandler.POST);

            if (jsonStr != null) {
                try {

                    JSONObject mainObject = new JSONObject(jsonStr);

                    JSONObject dataObject = mainObject.getJSONObject("data");
                    JSONObject cardObject = dataObject.getJSONObject("card");

                    parts = bat_order.split("`");

                    JSONObject b_1 = new JSONObject(parts[0]);

                    //Get innings extras
                    strextras=b_1.getString("extras");
                    strwide=b_1.getString("wide");
                    strnb=b_1.getString("noball");
                    strbye=b_1.getString("bye");
                    strlb=b_1.getString("legbye");

                    strtotal=b_1.getString("runs");
                    strRR=b_1.getString("run_rate");

                    //Get innings extras completed

                    JSONArray batting_order = b_1.getJSONArray("batting_order");
                    p_name = new ArrayList<String>();
                    for (int i = 0; i < batting_order.length(); i++) {
                        p_name.add(batting_order.getString(i));
                    }

                    target = b_1.getString("runs");
                    wicket = b_1.getString("wickets");

                    JSONObject teams = cardObject.getJSONObject("teams");

                    score=topScore.split(",");
                    inning_name=score[0];
                    inn_value=inning_name.split("_");
                    team_value = teams.names();
                    for(int i=0;i<team_value.length();i++){
                        if(team_value.getString(i).equals(inn_value[0])){
                            teamobject=teams.getJSONObject(team_value.optString(i));
                        }
                    }
                    team_name=teamobject.getString("short_name");
                    JSONObject matchobject=teamobject.getJSONObject("match");

                    JSONArray playing_Xi=matchobject.getJSONArray("playing_xi");

                    playing_11=new ArrayList<>();
                    p_name11=new ArrayList<>();

                    p_name11.addAll(p_name);
                    for(int i=0;i<playing_Xi.length();i++){
                        playing_11.add(playing_Xi.getString(i));
                    }
                    union=new ArrayList<>(p_name11);
                    union.addAll(playing_11);

                    intersection=new ArrayList<>(p_name11);
                    intersection.retainAll(playing_11);

                    symmentric=new ArrayList<>(union);
                    symmentric.removeAll(intersection);

                    JSONObject players = cardObject.getJSONObject("players");

                    JSONArray player_arr = players.names();

                    JSONObject player_data = null;
                    ArrayList<String> player_key = new ArrayList<>();
                    for (int i = 0; i < player_arr.length(); i++) {
                        player_key.add(player_arr.getString(i));
                    }

                    p_key = new ArrayList<JSONObject>();

                    for (int i = 0; i < batting_order.length(); i++) {
                        for (int j = 0; j < player_arr.length(); j++) {
                            if (batting_order.getString(i).equals(player_arr.getString(j))) {
                                p_key.add(players.getJSONObject(player_arr.optString(j)));
                            }
                        }
                    }

                    //BOWLLING PART START
                    JSONArray bowling_arr = b_1.getJSONArray("bowling_order");

                    lstbowling = new ArrayList<String>();
                    for (int i = 0; i < bowling_arr.length(); i++) {
                        lstbowling.add(bowling_arr.getString(i));
                    }

                    JSONObject players1 = cardObject.getJSONObject("players");

                    JSONArray player_arr1 = players1.names();

                    player_key1 = new ArrayList<String>();
                    for (int i = 0; i < player_arr1.length(); i++) {
                        player_key1.add(player_arr1.getString(i));
                    }

                    bowling_object = new ArrayList<JSONObject>();
                    for (int i = 0; i < bowling_arr.length(); i++) {

                        for (int j = 0; j < player_arr.length(); j++) {
                            if (bowling_arr.getString(i).equals(player_arr1.getString(j))) {
                                bowling_object.add(players1.getJSONObject(player_arr1.optString(j)));
                            }
                        }
                    }

                    fall_wicket = new ArrayList<String>();
                    JSONArray fall_wicket_data = b_1.getJSONArray("fall_of_wickets");
                    for (int i = 0; i < fall_wicket_data.length(); i++) {
                        fall_wicket.add(fall_wicket_data.getString(i));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else{
                Toast.makeText(getContext(), "There is no Data!!", Toast.LENGTH_SHORT).show();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
//            if (dialog.isShowing())
//                dialog.dismiss();

            txtyetBat.setText("Yet to bat:"+symmentric);
            txtBattingtotal.setText(team_name + "-" + target + "/" + wicket);
            txtextras.setText("Extras("+strextras+"):");
            txtwide.setText("W-"+strwide+",");
            txtNb.setText("Nb-"+strnb+",");
            txtBye.setText("B-"+strbye+",");
            txtLb.setText("Lb-"+strlb+",");
            txtTotal.setText("Total:"+strtotal+"/"+wicket);
            txtRR.setText("RR-"+strRR);

            lst.addHeaderView(batting_header, null, false);
            adapter=new FullScoreAdapter(getContext(),p_name,p_key);
            lst.setAdapter(adapter);
            Helper.getListViewSize(lst);

            lstBowling.addHeaderView(bowling_header, null, false);
            adapter_bowlilng=new FirstBowlingAdapter(getContext(),lstbowling,bowling_object);
            lstBowling.setAdapter(adapter_bowlilng);
            Helper.getListViewSize(lstBowling);

            lstWicket.addHeaderView(wicket_header, null, false);
            wicketAdapter=new WicketAdapter(getContext(),fall_wicket);
            lstWicket.setAdapter(wicketAdapter);
            Helper.getListViewSize(lstWicket);
        }
    }
}
package com.domore.tabfinaldemo;
导入android.app.ProgressDialog;
导入android.graphics.Typeface;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入Adapter.FirstBowlingAdapter;
导入Adapter.FullScoreAdapter;
导入Adapter.WicketAdapter;
公共类OneFragment扩展了Fragment{
视图根视图;
ListView lst、lstBowling、lstWicket;
FullScoreAdapter=null;
FirstBowlingAdapter\u Bowlling=null;
WicketAdapter WicketAdapter=null;
ArrayList p_name=null;
ArrayList p_key=null;
ArrayList bowling_对象=null;
ArrayList lstbowling=null;
ArrayList fall_wicket=null;
ArrayList player_key1=null;
ArrayList symmentric=null;
ArrayList union=null;
ArrayList相交=空;
ArrayList p_name11=null;
ArrayList播放_11=null;
查看击球头、保龄球头、边门头;
text视图txtBattingtotal;
字符串bat_order=“”;
字符串[]部分;
字符串[]分数;
字体tf;
字符串jsonStr=“”;
字符串target=“”;
弦导叶=”;
字符串[]inn_值;
字符串topScore=“”;
字符串局_name=“”;
字符串team_name=“”;
字符串yetBat=“”;
字符串strextras=“”,strwide=“”,strnb=“”,strbye=“”,strlb=“”,strtotal=“”,strrrr=“”;
JSONArray团队价值观;
JSONObject团队对象;
TextView-txtextras;
TextView-txtwide;
TextView-txtNb;
TextView-txtbeye;
TextView-txtLb;
文本视图txtTotal;
TextView-txtRR;
TextView txtyetBat;
私有静态最终字符串url=”http://10.0.2.2/JSON/fullList.php";
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
记分卡记分卡=(记分卡)getActivity();
bat_order=记分卡设置();
topScore=记分卡。设置得分();
rootView=充气机。充气(R.layout.fragment\u one,容器,false);
新建JSONLoader().execute();
txtextras=(TextView)rootView.findviewbyd(R.id.txtbExtras);
txtwide=(TextView)rootView.findViewById(R.id.txtbWide);
txtNb=(TextView)rootView.findviewbyd(R.id.txtbNb);
txtbeye=(TextView)rootView.findViewById(R.id.txtBye);
txtLb=(TextView)rootView.findviewbyd(R.id.txtbLegBye);
txtTotal=(TextView)rootView.findViewById(R.id.txtbTotal);
txtRR=(TextView)rootView.findviewbyd(R.id.txtbRR);
txtyetBat=(TextView)rootView.findviewbyd(R.id.txtyetBat);
tf=Typeface.createFromAsset(getActivity().getAssets(),“Roboto Medium.ttf”);
lst=(ListView)rootView.findViewById(R.id.ListView);
击球头=充气机。充气(右布局击球头,lst,假);
txtBattingtotal=(TextView)batting_header.findViewById(R.id.txtBattingtotal);
txtBattingtotal.setTypeface(tf);
lstBowling=(ListView)rootView.findviewbyd(R.id.listBowling);
保龄球头=充气机。充气(右布局。保龄球头,左保龄球,假);
lstWicket=(ListView)rootView.findViewById(R.id.listFallofWickets);
wicket_头=充气机。充气(右布局。wicket_头,lstWicket,假);
返回rootView;
}
公共类JSONLoader扩展异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//dialog=newprogressdialog(getActivity());
//setMessage(“获取…”);
//dialog.setUndeterminate(false);
//对话框。可设置可取消(true);
//dialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
ServiceHandler sh=新的ServiceHandler();
jsonStr=sh.makeServiceCall(url,ServiceHandler.POST);
if(jsonStr!=null){
试一试{
JSONObject mainObject=新的JSONObject(jsonStr);
JSONObject dataObject=mainObject.getJSONObject(“数据”);
JSONObject cardObject=dataObject.getJSONObject(“卡”);
零件=bat_订单分割(“`”);
JSONObject b_1=新的JSONObject(部件[0]);
//获得额外的局数
strextras=b_1.getString(“附加”);
strwide=b_1.getString(“宽”);
strnb=b_1.getString(“noball”);
strbye=b_1.getString(“bye”);
strlb=b_1.getString(“legbay”);
strtotal=b_1.getString(“运行”);
strRR=b_1.getString(“运行速率”);
//完成额外的局数
JSONArray击球顺序=b_1.getJSONArray(“击球顺序”);
p_name=new ArrayList();
对于(int i=0;i/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      hourText=(TextView)findViewById(R.id.hourText);
      minuteText=(TextView)findViewById(R.id.minuteText);


      //Create a broadcast receiver that will fire every minute
      tickReceiver=new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
        if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK)==0)
        {
           //you can call webservice here to retrive new data
        }

        }
      };

      //Register the broadcast receiver to receive TIME_TICK
      registerReceiver(tickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));

    }

    @Override
    public void onStop()
    {
      super.onStop();
      //unregister broadcast receiver.
      if(tickReceiver!=null)
      unregisterReceiver(tickReceiver);
    }
“Activity has leaked IntentReceiver that was originally registered here. Are you missing a call to unregisterReceiver()?”
if(youradapter == null)
 {
    youradapter = new CustomAdapter(this, yourList);
    listview.setAdapter(youradapter);

 }else{

     youradapter.setListArray(yourList); //<- provide a new data every minute here
     youradapter.notifyDataSetChanged();//<- this will update your UI with new data
  }