0

无论我尝试什么,我都无法让我的进度条工作。这是代码:

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());
        //

和 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" />

如果我关闭代码行 progressBar.setVisibility(ProgressBar.GONE); 然后我的进度条一直显示,所以它确实有效。

4

2 回答 2

2

我建议为此使用AsyncTask而不是您的线程实现(单击链接以获取文档中的清晰示例)。onPostExecute您可以在启动时使进度条可见并在方法中再次将其隐藏。我预计您的 while 循环会出现问题,这会更干净。

于 2013-11-09T19:08:22.877 回答
0

没关系,我可以通过在单独的线程上运行 while 循环来解决这个问题。在上面的代码中,它在 UI 线程上运行,因此所有用户界面都在循环执行时挂起。

于 2013-12-21T20:01:52.363 回答