ProgressDialog在安卓 [英] ProgressDialog in android

查看:98
本文介绍了ProgressDialog在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我怎么能显示progessDialog当数据被加载?

下面是我的活动code:

 包org.postandget;

进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.json *。
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;

公共类主要的扩展活动{
    静态的TextView电视;
    静态文本字符串;
    ProgressDialog progressDialog;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        电视=(TextView中)findViewById(R.id.textview);
        文本=;
        tv.setText(喜parthi);
       新main.execute();
    }

    公共静态无效POSTDATA(对象JSONfunctions)抛出JSONException {
        //创建一个新的HttpClient和门柱头球
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(http://eeeee.com/ee/login.php);
        JSONObject的JSON =新的JSONObject();
        尝试 {
            // JSON数据:
            json.put(名,法赫米·拉赫曼);
            json.put(位置,sysdev);
            JSONArray postjson =新JSONArray();
            postjson.put(JSON);
            //邮报数据:
            httppost.setHeader(JSON,json.toString());
            httppost.getParams()的setParameter(jsonpost,postjson)。

            //执行HTTP POST请求
            System.out.print(JSON);
            HTT presponse响应= httpclient.execute(httppost);
            //为JSON:
            如果(响应!= NULL)
            {
                InputStream的是= response.getEntity()的getContent()。
                的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
                StringBuilder的SB =新的StringBuilder();
                串线= NULL;
                尝试 {
                    而((行= reader.readLine())!= NULL){
                        sb.append(行+\ N);
                    }
                }赶上(IOException异常E){
                    e.printStackTrace();
                } 最后 {
                    尝试 {
                        is.close();
                    }赶上(IOException异常E){
                        e.printStackTrace();
                    }
                }
                文= sb.toString();
            }

            tv.setText(文本);

        }赶上(ClientProtocolException E){
            // TODO自动生成的catch块
        }赶上(IOException异常E){
            // TODO自动生成的catch块
        }
    }

    私有类的xyz扩展的AsyncTask<虚空,虚空,虚空> {
        私人最终ProgressDialog对话框=新ProgressDialog(main.this);

        在preExecute保护无效(){
            this.dialog.setMessage(请稍候...);
            this.dialog.show();
            // code这在preFIX时加载
            尝试 {
               main.postData(空);
            }赶上(JSONException E){
                e.printStackTrace(); }
        }

        @覆盖
        保护无效doInBackground(虚空......为arg0){

                //使您要在后台code

            返回null;
        }

        保护无效onPostExecute(最终虚空未使用){
            如果(this.dialog.isShowing()){
                this.dialog.dismiss();

            }

        }
    }
}
 

解决方案

您需要使用的AsyncTask

的AsyncTask能够适当且易于使用的用户界面线程。该类允许无需操作线程和/或处理程序进行后台操作并发布在UI线程上的结果。

这是异步任务是由在后台线程运行的计算,其结果发表在UI线程上定义。异步任务由3泛型类型,名为PARAMS,进展和成效,以及4个步骤定义,呼吁preExecute,doInBackground,onProgressUpdate和onPostExecute

 私有类XYZ扩展的AsyncTask<虚空,虚空,虚空> {
    私人最终ProgressDialog对话框=新ProgressDialog(main.this);

    在preExecute保护无效(){
        this.dialog.setMessage(请稍候...);
        this.dialog.show();
        // code这在preFIX时加载

    }

    @覆盖
    保护无效doInBackground(虚空......为arg0){

            //使您要在后台code

        返回null;
    }

    保护无效onPostExecute(最终虚空未使用){
        如果(this.dialog.isShowing()){
            this.dialog.dismiss();

        }

    }
}
 

和使用本在你的按钮单击事件或主文件::

 新的xyz()执行()。
 

更新:

 包org.postandget;

进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.json *。
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;

公共类主要的扩展活动{
    TextView的电视;
    文本字符串;
  ProgressDialog progressDialog;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        电视=(TextView中)findViewById(R.id.textview);
        文本=;
        tv.setText(喜parthi);
       新main.execute();
    }
    公共无效POSTDATA(对象JSONfunctions)抛出JSONException {
        //创建一个新的HttpClient和门柱头球
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(http://eeeee.com/ee/login.php);
        JSONObject的JSON =新的JSONObject();
        尝试 {
            // JSON数据:
            json.put(名,法赫米·拉赫曼);
            json.put(位置,sysdev);
            JSONArray postjson =新JSONArray();
            postjson.put(JSON);
            //邮报数据:
            httppost.setHeader(JSON,json.toString());
            httppost.getParams()的setParameter(jsonpost,postjson)。

            //执行HTTP POST请求
            System.out.print(JSON);
            HTT presponse响应= httpclient.execute(httppost);
            //为JSON:
            如果(响应!= NULL)
            {
                InputStream的是= response.getEntity()的getContent()。
                的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
                StringBuilder的SB =新的StringBuilder();
                串线= NULL;
                尝试 {
                    而((行= reader.readLine())!= NULL){
                        sb.append(行+\ N);
                    }
                }赶上(IOException异常E){
                    e.printStackTrace();
                } 最后 {
                    尝试 {
                        is.close();
                    }赶上(IOException异常E){
                        e.printStackTrace();
                    }
                }
                文= sb.toString();
            }

            tv.setText(文本);

        }赶上(ClientProtocolException E){
            // TODO自动生成的catch块
        }赶上(IOException异常E){
            // TODO自动生成的catch块
        }
    }
    私有类的xyz扩展的AsyncTask<虚空,虚空,虚空> {
        私人最终ProgressDialog对话框=新ProgressDialog(main.this);

        在preExecute保护无效(){
            this.dialog.setMessage(请稍候...);
            this.dialog.show();
            // code这在preFIX时加载
            尝试 {
                POSTDATA(savedInstanceState);
            }赶上(JSONException E){
                e.printStackTrace(); }
        }

        @覆盖
        保护无效doInBackground(虚空......为arg0){

                //使您要在后台code

            返回null;
        }

        保护无效onPostExecute(最终虚空未使用){
            如果(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();

            }

        }
    }
}

解决方案

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

            }

        }
    }
}

这篇关于ProgressDialog在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆