运行AsyncTask

时间:2017-05-15 07:53:34

标签: android android-asynctask android-progressbar

我有AsyncTask正在做一些背景“工作”。

ProgressBaronPreExecute中实施onPostExecute,相应地更改了可见性。

但我无法看到ProgressBar。完成工作后屏幕会冻结几秒钟,然后活动会加载数据。

我通过改变ProgressBar的背景颜色进行测试,当改变bgcolor(告诉ProgressBar在那里)时,我可以看到ProgressBar为方形黑色视图,但这不是要求。

我如何看到ProgressBar的微调器,我也发布了我的布局代码,以防万一有错:

xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coor"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f4f4f4"
        android:fitsSystemWindows="true"
        tools:context="com.example.svatts.my22.ScrollingActivity">


        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:fitsSystemWindows="true"
                app:contentScrim="?android:attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <!--
                // image view was here-->
                <TextView
                    android:id="@+id/userName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="20dp"
                    android:paddingTop="20dp"
                    android:textColor="#000"
                    android:textSize="20sp"
                    app:layout_collapseMode="parallax" />

            </android.support.design.widget.CollapsingToolbarLayout>


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp">

                <ImageView

                    android:id="@+id/fbdp"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"

                    />

                <TextView
                    android:id="@+id/month"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="20dp"
                    android:layout_toRightOf="@+id/fbdp"
                    android:fontFamily="cursive"
                    android:text="December ' 16"
                    android:textColor="#000"
                    android:textSize="15sp" />


                <TextView

                    android:id="@+id/amt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/month"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_toRightOf="@+id/fbdp"
                    android:textColor="#000"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </RelativeLayout>


            <!--need to change the background as an object oval prbbly-->
        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_scrolling" />

    </android.support.design.widget.CoordinatorLayout>

    <ProgressBar
        android:id="@+id/loadingBar"
        style="@style/Widget.AppCompat.ProgressBar"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_gravity="center"
        android:theme="@style/CircularProgress"
        android:visibility="visible" />

</FrameLayout>

AsyncTask:

    public class ReadingSMS extends AsyncTask<String, String, Cursor> {

        protected void onPreExecute() {
            mLoadingIndicator.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(Cursor result) {
            run(result);
            mLoadingIndicator.setVisibility(View.INVISIBLE);

        }

        @Override
        protected Cursor doInBackground(String... params) {

            return refreshSmsInbox();

        }
    }

}

CircularProgress风格:

 <style name="CircularProgress" parent="Theme.AppCompat.Light">
        <item name="colorAccent">@color/colorFAB2</item>
    </style>

run()方法:

@Override
public void run(Cursor result) {
    int indexDate = result.getColumnIndex("date");
    int indexBody = result.getColumnIndex("body");

    do {
        //  String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
        //        "\n" + smsInboxCursor.getString(indexBody) + "\n";
        //    arrayAdapter.add(str);

        if (!stopReading) {
            Date date = new Date(Long.parseLong(result.getString(indexDate)));


            String newDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(date);
            //DateTime fmt = DateTime.parse(date.toString(),DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss z yyyy"));
            //String formattedDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").format(date);

            // Toast.makeText(this,newDate,Toast.LENGTH_SHORT).show();

            Transaction tr = new Transaction(ScrollingActivity.this);
            if (tr.isTransaction(result.getString(indexBody))) {
                Log.d("here now bro", "yea");
                tr.msg = result.getString(indexBody);
                tr.date = newDate;
                tr.call(ScrollingActivity.this);


            }
        }
    } while (result.moveToNext());


    ////////////////////////////////
    trList = new ArrayList<Transaction>();

    // trList = new Transaction(this).getTransactions();  // getting transactions from pythonanywhere server

    trList = new Transaction(this).getTransactionsFromSQLite();

    Transaction.transactionList = trList;

    getPreviousMonthsInfo(trList);
    updateBarChart(expenses);
    Log.d("gettingVals", "yooyyo");



    setRecentTrans();

    TextView month = (TextView) findViewById(R.id.month);
    month.setText(monthNames[0] + "-" + currentYear);
    month.setTypeface(sansation);

    TextView amt = (TextView) findViewById(R.id.amt);
    month.setTypeface(sansation);
    amt.setText("₹ " + expenses[0]);

    TextView recentHeading = (TextView) findViewById(R.id.recentHeading);
    month.setTypeface(sansation);


    TextView graphHeading = (TextView) findViewById(R.id.graphHeading);
    month.setTypeface(sansation);

    setBalances();
}

0 个答案:

没有答案