无法获得旋转进度条工作

时间:2013-11-09 19:01:16

标签: java android progress

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

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);然后我的进度条会一直显示,所以它确实有效。

2 个答案:

答案 0 :(得分:2)

我建议使用AsyncTask代替你的Thread实现(点击链接以获取文档中的明确示例)。您可以在启动时使进度条可见,并在onPostExecute方法中再次隐藏它。我希望你的while循环有一些问题,这会更清晰。

答案 1 :(得分:0)

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