0

I am having two tasks running in doInBackground(). One task is reading data from site and another task is parsing xml file from Url and set image in imageview. But i want to separate both tasks means once the reading from the site is completed , it should set text in textview. and after that xml parsing should start. So what can i do ?

Here is my code :

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

How should i handle these two tasks ? Please suggest me some solution so that it takes minimum time. Please reply me fast. Thanx in advance.

Zankhna
  • 4,570
  • 9
  • 62
  • 103
  • after first asynctask is complete you will get result in onPostExecuate() just start another asynctask – Sumant Dec 10 '12 at 10:22

1 Answers1

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.

dave.c
  • 10,910
  • 5
  • 39
  • 62
  • I have done with first approach. But when i tried with second approach,my view get set after completing both task which i dont want. So have done with first approach, which set my view after completing downloading. So thanks mate. U really help me. – Zankhna Dec 10 '12 at 15:33