文本视图中的自动刷新

时间:2014-01-12 16:17:11

标签: android

我是android新手。 在这里,我试图制作一个应用程序,从Json中的Web获取数据,并在我尝试自动刷新活动时显示它,它显示旧数据和新数据两者都可以帮助我。

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    resultview1=(TextView) findViewById(R.id.team1_1);
    resultview2=(TextView) findViewById(R.id.team1_2);
    resultview3=(TextView) findViewById(R.id.date);
    resultview4=(TextView) findViewById(R.id.comm1);
    resultview5=(TextView) findViewById(R.id.team2_1);
    resultview6=(TextView) findViewById(R.id.team2_2);
    resultview7=(TextView) findViewById(R.id.comm2);
    resultview8=(TextView) findViewById(R.id.team3_1);
    resultview9=(TextView) findViewById(R.id.team3_2);
    resultview10=(TextView) findViewById(R.id.comm3);
    resultview11=(TextView) findViewById(R.id.team4_1);
    resultview12=(TextView) findViewById(R.id.team4_2);
    resultview13=(TextView) findViewById(R.id.comm4);




    new BackHelper().execute();
    //mHandler();


}

/*private void mHandler() 
{
    // TODO Auto-generated method stub
    mHandler.postDelayed(updateTask, 15000);
} */





public void  getdata1()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s1= s1 +
                    "Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
                       "Over : "+json.getInt("t1_0");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}





public void  getdata2()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s2= s2 +
                    "Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
                       "Over : "+json.getInt("t2_o");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}



public void  getdata3()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s3= s3 
                    +json.getString("date");


        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}




public void  getdata4()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s4= s4 
                    +json.getString("comm");


        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}



public void  getdata5()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team2.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s5= s5 +
                    "Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
                       "Over : "+json.getInt("t1_0");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}


public void  getdata6()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team2.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s6= s6 +
                    "Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
                       "Over : "+json.getInt("t2_o");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}



public void  getdata7()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team2.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s7= s7+json.getString("comm");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}



public void  getdata8()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team3.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s8= s8 +
                    "Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
                       "Over : "+json.getInt("t1_0");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}



public void  getdata9()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team3.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s9= s9 +
                    "Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
                       "Over : "+json.getInt("t2_o");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}

public void  getdata10()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team3.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s10= s10 +json.getString("comm");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}


public void  getdata11()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team4.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s11= s11 +
                    "Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
                       "Over : "+json.getInt("t1_0");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}

public void  getdata12()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team4.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s12= s12 +
                    "Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
                       "Over : "+json.getInt("t2_o");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}



public void  getdata13()
{
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = (HttpClient) new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team4.php");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        isr= entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
        //resultview.setText("could not connect to database");
    }
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) !=null)
        {
            sb.append(line + "\n");
        }
        isr.close();
        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag","Error converting result"+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0; i<jArray.length();i++)
        {
            JSONObject json = jArray.getJSONObject(i);
            s13= s13 +json.getString("comm");

        }

        //resultview.setText(s);

    }catch (Exception e){
        Log.e("log_tag","Error Parsing Data"+e.toString());

    }
}

/ * private Handler mHandler = new Handler();

private Runnable updateTask = new Runnable () 
{
    public void run() 
    {
        Log.d(getString(R.string.app_name) + " ChatList.updateTask()",
                "updateTask run!");

                    // run any code here...
        //getdata();
        new BackHelper().execute();
        Log.e("hell", "hello");
        // queue the task to run again in 15 seconds...
        mHandler.postDelayed(updateTask, 15000);
    }
};*/
/*private final static int Interval=1000;
Handler mHandler;
 Runnable mHandlerTask=new Runnable() {

    public void run() {
        // TODO Auto-generated method stub
         new BackHelper().execute();
         mHandler.postDelayed(mHandlerTask, Interval);
    }
};
void startrepeatingTask()
{
    mHandlerTask.run();
}
void stopRepeatingTask()
{
    mHandler.removeCallbacks(mHandlerTask);
}*/





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

    @Override
    protected Void doInBackground(Void... params) 
    {
        getdata1();
        getdata2();
        getdata3();
        getdata4();
        getdata5();
        getdata6();
        getdata7();
        getdata8();
        getdata9();
        getdata10();
        getdata11();
        getdata12();
        getdata13();



        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //super.onPostExecute(result1);
        resultview1.setText(s1);
        resultview2.setText(s2);
        resultview3.setText(s3);
        resultview4.setText(s4);
        resultview5.setText(s5);
        resultview6.setText(s6);
        resultview7.setText(s7);
        resultview8.setText(s8);
        resultview9.setText(s9);
        resultview10.setText(s10);
        resultview11.setText(s11);
        resultview12.setText(s12);
        resultview13.setText(s13);



    }






}

}

2 个答案:

答案 0 :(得分:0)

如果同时显示旧数据和新数据,则应在代码中使用setText代替append方法。这仅适用于您的更新方法。我不知道您是一行一行还是整篇文章,在第一种情况下,您只需要为第一行调用setText,然后继续使用append休息,如果是第二种情况,setText就可以了。

答案 1 :(得分:0)

如果我没有错,你之前已经问了同样的问题。正如我之前告诉过您的那样,您正在使用旧信息附加新信息。例如

s2= s2 +"Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
                       "Over : "+json.getInt("t2_o");

然后您将由旧数据和新数据组成的s2设置为文本视图,从而导致显示两个数据。您必须将s2的值设置为s2="",以确保不会继续使用旧值。