robs
robs

Reputation: 659

ProgressDialog showing up after XML is fetched

Hey, I'm trying to get a ProgressDialog working while im fetching a XML from the web. The problem is that the dialog shows up as soon as the XML is loaded and displayed. If I dismiss it afterwards it won't show up at all. Any ideas what to do? I'm new to android development and this is my first app, I've already searched the net but didn't find any working solution.. Here is the shortened code:

public class showReleases extends Activity {
    SitesList sitesList = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.releasedetails);
    ProgressDialog dialog = new ProgressDialog(showReleases.this);
    dialog.setMessage("please wait");
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.show();

    getFeed2();

    dialog.dismiss();
}


public void getFeed2() {
    Bundle extras = getIntent().getExtras();

    try {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        URL sourceUrl = new URL("http://www.it-leaked.com/app/details.php?id=" + extras.getString("id"));

        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    sitesList = MyXMLHandler.sitesList;

    TextView txtView = (TextView)findViewById(R.id.TextView01);
    txtView.setText(sitesList.getTitle().get(0));
    TextView txtView2 = (TextView)findViewById(R.id.TextView02);
    String myTracklist = sitesList.getTracklist().get(0);
    myTracklist = myTracklist.replace("||||", "\n");
    txtView2.setText(myTracklist);
}
}

Thanks in advance

Upvotes: 0

Views: 473

Answers (2)

Hefferwolf
Hefferwolf

Reputation: 1

To create a progress dialog, you should override "onCreateDialog" in your Activity:

@Override
protected Dialog onCreateDialog(int id) {
    ProgressDialog dialog = new ProgressDialog(this);
    dialog.setMessage("loading...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(true);
    return dialog;
}

and display it using "showDialog" before loading the xml data:

showDialog(0);

and "removedDialog" to get rid of it:

context.removeDialog(0);

Upvotes: 0

Andrew White
Andrew White

Reputation: 53516

Read up on AsynTask, it is designed for just this use-case.

Upvotes: 2

Related Questions