簡體   English   中英

AsyncTask顯示進度條

[英]AsyncTask displaying progress bar

我對android非常陌生。 我進行了兩次活動A,B。 活動A解析服務器中的數據並遍歷各個級別。 並通過意圖調用活動B。 活動B需要一些時間來顯示數據,因此我試圖顯示進度條。 這是我的代碼。

 public class Display extends Activity  {

        ProgressDialog dialog;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.attributequestions);

            new asynctask().execute();


        }

        class asynctask extends AsyncTask<Context,Void,Void>{
            Survey[] surveyque=null;
    // i hace created seperated class forsurvey that has info about data
            String list[];  


            private ProgressDialog Dialog;
            @Override
            protected void onPreExecute()
            {
                Dialog=ProgressDialog.show(Display.this, "Parsing Data", "Please wait..........");


            }
            @Override
            protected void onPostExecute(Void unused)    
            {
                try
                {
                    if(Dialog.isShowing())
                    {
                        Dialog.dismiss();
                    }
                    Intent intent=getIntent();

                }
                catch(Exception e)
                {
    Log.d("Onsitev4", "error");
                }
            }

            @Override
            protected Void doInBackground(Context... params) {
                try {

                    LinearLayout layout1 = (LinearLayout) findViewById(R.id.linearLayout1);

    //getting exception here. I dont understant why
    // I have declared layout params and displaying activities in another class 

                    ButtonView c = new ButtonView();                
                    c.layout=layout1;
                    c.context =getBaseContext();


                    DbCoreSqlSurveys surveys=new DbCoreSqlSurveys(getBaseContext());
                    Document doc =surveys.getSurveySet();
                    surveyquestions= GetSurveyLevels(doc,c );


                } catch (TransformerFactoryConfigurationError e) {
                    e.printStackTrace();
                }
                return null;
            } 

        }

        public SurveyObject[] GetSurveyLevels(Document doc, ButtonView c) {
            NodeList nlQuestions = doc.getElementsByTagName("Survey");
            SurveyObject[] allsurveys = new SurveyObject[nlQuestions.getLength()];


            for (int i = 0; i < nlQuestions.getLength(); i++){

                Node survey =  nlQuestions.item(i);
                String f =survey.getNodeName();
                Log.d("OnsiteV4", "survey " + f);

                NodeList surveyChildNodes = survey.getChildNodes();
                SurveyObject s=new SurveyObject();

                for (int j = 0; j < surveyChildNodes.getLength(); j++){

                    Node surveyChild =  surveyChildNodes.item(j);               
                    String h =surveyChild.getNodeName();
                    Log.d("OnsiteV4", "survey child node = " + h);

                    if (h !="#text"){
                        Surveys t = Surveys.valueOf(h); 

                        switch(t){
                        case KeySurvey:
                            s.KeySurvey=surveyChild.getTextContent();
                            displaySurveyLink(s.SurveyDescription,"",c,0,s.SurveyDescription,"","","","");
                            break;
                        case SurveyDescription:
                            s.SurveyDescription=surveyChild.getTextContent();
                            displaySurveyLink(s.SurveyDescription,"",c,0,s.SurveyDescription,"","","","");
                            break;
                        case SurveyUserCode:
                            s.SurveyUserCode=surveyChild.getTextContent();
                            break;
                        case Level1:
                            if(surveyChild.hasChildNodes()){
                                s.Level1=   processLevel1Nodes(surveyChild,c,s.SurveyDescription);
                            }
                            break;
                        default:
                            break;
                        }
                    }

                    allsurveys[i]=s;
                }
            }

            return allsurveys;
        }

    // methods iterating through levels that is not showed

        private  void displaySurveyLink(final String description, String tag, ButtonView c, int indentation, final String surveyDescription, final String level1description, final String level2description, final String level3description, final String level4description)
        {
            if (description == null || tag == null){
                return;
            }
            final TextView tv = c.addButton(description,tag,indentation);


            tv.setOnClickListener(new OnClickListener(){
                public void onClick(View v) {

                    final Intent intent = new Intent();
                    intent.setClass(v.getContext(),ActivityB.class);
                    intent.putExtra("KeyLevel",tv.getTag().toString()); 

                    intent.putExtra("SurveyDescription",surveyDescription);
                    intent.putExtra("level1description",level1description);
                    intent.putExtra("level2description",level2description);
                    intent.putExtra("level3description",level3description);
                    intent.putExtra("level4description",level4description);
                    intent.putExtra("Description",description);

                    if (tv.getTag() != null){
                        if (tv.getTag().toString() != ""){
                            startActivity(intent);
                        }


                    }
                }


            });
        }
    }  

我在doinbackground中遇到異常。 我很困惑 。 請幫我..

因為在asyc任務的doinbackgound中執行UI時,您的代碼應該引發異常。 請從doingbackgound方法中刪除所有與UI相關的工作。

由於您正在訪問非UI線程上的UI元素,因此出現異常。 應用程序創建的主線程是UI線程,這是創建所有可視元素的位置,因此是您應該訪問它們的唯一線程。

要正確使用AsyncTask,請在doInBackground運行長時間運行的操作,並使用onPreExecuteonPostExecuteonProgressUpdated與UI配合使用(顯示/隱藏進度對話框,更新視圖等)。 每當我使用AsyncTask並希望顯示進度時,我都會覆蓋onProgressUpdated並為其賦予參數類型Integer,然后從doInBackground調用publishProgress。 這將需要將基類簽名從AsyncTask<Context,Void,Void>更改為AsyncTask<Context,Integer,Void> 您也可以為此使用其他對象類型...例如,如果您想顯示已完成任務的百分比,我僅以Integer為例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM