0

doInBackground() で 2 つのタスクを実行しています。1 つのタスクはサイトからデータを読み取り、別のタスクは URL から xml ファイルを解析して imageview に画像を設定します。しかし、両方のタスクを分離したいのは、サイトからの読み取りが完了したら、textview にテキストを設定する必要があることを意味します。その後、xml 解析が開始されます。それで、なにかお手伝いできますか ?

これが私のコードです:

private class DownloadWebPageTask extends AsyncTask<String, Void, ArrayList<String>>
    {
        ProgressBar progressBar;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressBar = (ProgressBar)findViewById(R.id.showLoading);
            progressBar.bringToFront();         

        }

        @Override
        protected ArrayList<String> doInBackground(String... arg0)
        {
            String xml = null;

            ArrayList<String> lyricArtistArray = new ArrayList<String>();

                    //get lyrics - First task
            lyricsArray = SearchHelper.findLyrics(arg0[0],arg0[1]);
            lyricArtistArray.add(lyricsArray);

                //get image - Second task
            artistNameGlobal = arg0[1].toString();
            try
            {
                XMLParser parser= new XMLParser();
                String img;
                String t = arg0[1].replace(" ","%20");
                String url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist="+t+"&api_key=mykey";
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                xml = EntityUtils.toString(httpEntity);
                Document doc = parser.getDomElement(xml);
                NodeList nl = doc.getElementsByTagName("artist");
                for(int i=0;i<1;i++)
                {
                    Element e = (Element)nl.item(i);
                    img = parser.getValue(e,"image");
                    System.out.print(img);
                    ImageView imageArtist = (ImageView) findViewById(R.id.artistImageView);
                    imageArtist.setImageBitmap(LoadImageFromURL(img));
                }               
            }
            catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

            return lyricArtistArray;
        }

        @Override
        protected void onPostExecute(ArrayList<String> lyricArtistArray)
        {
            progressBar.setVisibility(View.GONE);

            tvMain.setTextSize(fontSize);
            tvMain.setTextColor(FontColor);
            if(!FontStyle.equals("None"))
            {
                String s = FontStyle.concat(".ttf");
                String s1 = "fonts/"+s;
                Typeface tf=Typeface.createFromAsset(getAssets(),s1);
                tvMain.setTypeface(tf); 
            }

            tvMain.setText(Html.fromHtml(lyricArtistArray.get(0).toString()));
            tvMain.setMovementMethod(new ScrollingMovementMethod());                    
            tvMain.setSelected(true);       
        }
    }

これら2つのタスクをどのように処理すればよいですか? 最小限の時間がかかるように、解決策を提案してください。はやく返信してください。事前にサンクス。

4

1 に答える 1

0

A couple of approaches come to mind. The first is to implement onProgressUpdate(Progress...) passing a String and set the value of the TextView within this method. You would then call updateProgress after your download is complete, but before the parse starts. This would be the minimum amount of work on you part to get this working.

The other route is to create a new AsyncTask with just the parse code in it. The first AsyncTask (which would now just perform the download) will set the TextView content and start the parsing AsyncTask in its onPostExecute.

于 2012-12-10T10:29:36.723 に答える