Android 中的进度对话框

发布于 2024-12-04 20:34:22 字数 3901 浏览 3 评论 0原文

这是我的活动代码..这里我通过 JSON 和 PHP 从数据库获取数据...

如何在加载数据时显示 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();

            }

        }
    }
}

This is my activity code.. here I'm fetching data from the database through JSON and PHP...

How can I display a progessDialog when the data is loading?

Here is my activity code:

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();

            }

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘逸的'云 2024-12-11 20:34:22

您需要使用 asyncTask

AsyncTask 可以正确且轻松地使用 UI 线程。此类允许在 UI 线程上执行后台操作并发布结果,而无需操作线程和/或处理程序。

异步任务由在后台线程上运行的计算定义,其结果在 UI 线程上发布。异步任务由 3 个通用类型(称为 Params、Progress 和 Result)和 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();

更新:

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();

            }

        }
    }
}

you need to use asyncTask

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and 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();

        }

    }
}

and use this in your button click event or in main file ::

 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();

            }

        }
    }
}
小草泠泠 2024-12-11 20:34:22

首先在您的活动中创建一个进度对话框对象,如下所示---->

  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;
}

First create an object of Progress Dialog in your activity like this ---->

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

then within your Try block where you posting the data show the progress dialog like this ---->

  1. showDialog(Dialog_id);

and after your onCreate() method create an onCreateDialog() method like this ----->

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文