2011-09-14 105 views
-1

這是我的活動代碼..我在這裏從通過JSON和PHP數據庫中獲取數據...ProgressDialog在android系統

我怎麼能顯示progessDialog數據加載時?

這裏是我的活動代碼:

package org.postandget; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.HashMap; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.*; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class main extends Activity { 
    static TextView tv; 
    static String text; 
    ProgressDialog progressDialog; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);  
     tv = (TextView)findViewById(R.id.textview); 
     text = ""; 
     tv.setText("hi parthi");     
     new main.execute(); 
    } 

    public static void postData(Object JSONfunctions) throws JSONException{ 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://eeeee.com/ee/login.php");  
     JSONObject json = new JSONObject(); 
     try { 
      // JSON data:   
      json.put("name", "Fahmi Rahman"); 
      json.put("position", "sysdev");   
      JSONArray postjson=new JSONArray(); 
      postjson.put(json); 
      // Post the data: 
      httppost.setHeader("json",json.toString()); 
      httppost.getParams().setParameter("jsonpost",postjson); 

      // Execute HTTP Post Request 
      System.out.print(json); 
      HttpResponse response = httpclient.execute(httppost); 
      // for JSON: 
      if(response != null) 
      { 
       InputStream is = response.getEntity().getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       try { 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        try { 
         is.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       text = sb.toString(); 
      } 

      tv.setText(text); 

     }catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 
    } 

    private class xyz extends AsyncTask<Void, Void, Void> { 
     private final ProgressDialog dialog = new ProgressDialog(main.this); 

     protected void onPreExecute() { 
      this.dialog.setMessage("Please Wait..."); 
      this.dialog.show(); 
      //code which load at prefix time 
      try { 
       main.postData(null);   
      } catch (JSONException e) { 
       e.printStackTrace();  } 
     } 

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

       // make code which you want in background 

      return null; 
     } 

     protected void onPostExecute(final Void unused) { 
      if (this.dialog.isShowing()) { 
       this.dialog.dismiss(); 

      } 

     } 
    } 
} 

回答

1

你需要使用的AsyncTask

的AsyncTask能夠正確且容易使用的UI線程。該類允許執行後臺操作並在UI線程上發佈結果,而無需操縱線程和/或處理程序。

異步任務由在後臺線程上運行並且其結果在UI線程上發佈的計算定義。異步任務由3種泛型類型,稱爲PARAMS,進展和結果,以及4個步驟定義,所謂onPreExecute,doInBackground,onProgressUpdate和onPostExecute

 private class xyz extends AsyncTask<Void, Void, Void> { 
    private final ProgressDialog dialog = new ProgressDialog(main.this); 

    protected void onPreExecute() { 
     this.dialog.setMessage("Please Wait..."); 
     this.dialog.show(); 
     //code which load at prefix time 

    } 

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

      // make code which you want in background 

     return null; 
    } 

    protected void onPostExecute(final Void unused) { 
     if (this.dialog.isShowing()) { 
      this.dialog.dismiss(); 

     } 

    } 
} 

並在按鈕單擊事件或主文件中使用此: :

new xyz().execute(); 

UPDATE:

package org.postandget; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.HashMap; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.*; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class main extends Activity { 
    TextView tv; 
    String text; 
    ProgressDialog progressDialog; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);  
     tv = (TextView)findViewById(R.id.textview); 
     text = ""; 
     tv.setText("hi parthi");     
     new main.execute(); 
    } 
    public void postData(Object JSONfunctions) throws JSONException{ 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://eeeee.com/ee/login.php");  
     JSONObject json = new JSONObject(); 
     try { 
      // JSON data:   
      json.put("name", "Fahmi Rahman"); 
      json.put("position", "sysdev");   
      JSONArray postjson=new JSONArray(); 
      postjson.put(json); 
      // Post the data: 
      httppost.setHeader("json",json.toString()); 
      httppost.getParams().setParameter("jsonpost",postjson); 

      // Execute HTTP Post Request 
      System.out.print(json); 
      HttpResponse response = httpclient.execute(httppost); 
      // for JSON: 
      if(response != null) 
      { 
       InputStream is = response.getEntity().getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       try { 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        try { 
         is.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       text = sb.toString(); 
      } 

      tv.setText(text); 

     }catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 
    } 
    private class xyz extends AsyncTask<Void, Void, Void> { 
     private final ProgressDialog dialog = new ProgressDialog(main.this); 

     protected void onPreExecute() { 
      this.dialog.setMessage("Please Wait..."); 
      this.dialog.show(); 
      //code which load at prefix time 
      try { 
       postData(savedInstanceState);   
      } catch (JSONException e) { 
       e.printStackTrace();  } 
     } 

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

       // make code which you want in background 

      return null; 
     } 

     protected void onPostExecute(final Void unused) { 
      if (this.dialog.isShowing()) { 
       this.dialog.dismiss(); 

      } 

     } 
    } 
} 
+0

我不能在..... 「tranning.this」 得到這個line2 – Parthi04

+0

在這裏你使用「main.this」 –

+0

我有更新我的代碼,請考慮它。基本上它屬於你現在的班級名稱 –

0

首先CR eate進度對話框的對象在你的活動這樣---->

  1. ProgressDialog pd;
  2. static final int Dialog_id = 1;

那麼,你發佈的數據顯示進度對話框你這樣的try塊中 - --->

  1. showDialog(Dialog_id);

後您的onCreate()方法創建一個onCreateDialog()方法,這樣----->

4.

protected Dialog onCreateDialog(int id){ 
     switch(id){ 
      case Dialog_id : 
      ProgressDialog pd = new ProgressDialog(this); 
      pd.setTitle("Loading Data"); 
      pd.setCancelable(true); 
      return pd; 
      break; 
     return null; 
} 
+0

我已經使用ur代碼..它是有幫助的...但我得到對話框數據從數據庫中提取.. NOt之前的數據庫... – Parthi04

+0

所以把showDialog(Dialog_id)方法之前,你的代碼部分將數據輸入數據庫。 – Rocker

+0

如果我的代碼或答案幫助你,請upvote我的答案。 – Rocker