简体   繁体   中英

Can't get spinning Progress Bar to work

No matter what I try I can't get my progress bar to work. Here is the code:

public void setDayView(final int _day, final int _month, final int _year) {
    ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
    progressBar.setVisibility(ProgressBar.VISIBLE);
    comm = new Thread() {

        public void run() {
            final postRequest req = new postRequest(username, password);
            doc = (Document) req.GetDayEvents(_day, _month, _year);
        }
    };
    comm.start();
    while (comm.isAlive()) { // waiting for network to finish

    }
    progressBar.setVisibility(ProgressBar.GONE);
    NodeList eventsTitles = doc.getElementsByTagName("Title");
    NodeList eventsClasses = doc.getElementsByTagName("Class");
    NodeList eventsTypes = doc.getElementsByTagName("Type");
    NodeList eventsComments = doc.getElementsByTagName("Comment");
    NodeList eventsAmounts = doc.getElementsByTagName("Amount");
    NodeList eventsHashes = doc.getElementsByTagName("Hash");
    NodeList refCurrency = doc.getElementsByTagName("RefCurrency");
    NodeList dateStamp = doc.getElementsByTagName("DateStamp");

    final int nrOfEvents = eventsTitles.getLength();
    LinearLayout dayEventsArea = (LinearLayout) findViewById(R.id.DayEventsArea);

    // set SelectedDayLabel
    TextView selDayLabel = (TextView) findViewById(R.id.selectedDayLabel);
    selDayLabel.setText(dateStamp.item(0).getTextContent());
    //

    // set day totals labels
    TextView dayDebit = (TextView) findViewById(R.id.dayDebitLabel);
    TextView dayOutgo = (TextView) findViewById(R.id.dayOutgoLabel);
    TextView dayIncome = (TextView) findViewById(R.id.dayIncomeLabel);
    dayDebit.setText("Debit: "
            + doc.getElementsByTagName("DayDebit").item(0).getTextContent()
            + " " + refCurrency.item(0).getTextContent());
    dayOutgo.setText("Outgo: "
            + doc.getElementsByTagName("DayOutgo").item(0).getTextContent()
            + " " + refCurrency.item(0).getTextContent());
    dayIncome.setText("Income: "
            + doc.getElementsByTagName("DayIncome").item(0)
                    .getTextContent() + " "
            + refCurrency.item(0).getTextContent());
    //
    dayEventsArea.removeAllViews();

    // clearing all local caches
    _eventsTitlesLocalCopy.clear();
    _eventsClassesLocalCopy.clear();
    _eventsAmountsLocalCopy.clear();
    _eventsCommentsLocalCopy.clear();
    _eventsTypesLocalCopy.clear();
    _eventsHashesLocalCopy.clear();
    //
    for (int i = 0; i < eventsTitles.getLength(); i++) {
        // creating a local cache of the day events, as they can be edited
        // and sent back
        _eventsTitlesLocalCopy.add(eventsTitles.item(i).getTextContent());
        _eventsClassesLocalCopy.add(eventsClasses.item(i).getTextContent());
        _eventsAmountsLocalCopy.add(eventsAmounts.item(i).getTextContent());
        _eventsCommentsLocalCopy.add(eventsComments.item(i)
                .getTextContent());
        _eventsTypesLocalCopy.add(eventsTypes.item(i).getTextContent());
        _eventsHashesLocalCopy.add(eventsHashes.item(i).getTextContent());
        //

and code from layout.xml

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:layout_gravity="center" />

If I turn off code line progressBar.setVisibility(ProgressBar.GONE); then my progress bar is shown all the time, so it actually works.

I would suggest using an AsyncTask for this instead of your Thread-implementation (click on the link for a clear example in the documentation). You can make the progress bar visible when initiating and hide it again in the onPostExecute method. I expect some problem with your while-loop, this would be much cleaner.

Nevermind, I was able to resolve this by running while loop on a separate thread. In the code above it runs on UI thread so all the user interface just hangs up while the loop is executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM