进度循环没有运行,直到加载RecyclerView

时间:2016-10-12 13:14:28

标签: android android-recyclerview progress-bar

我正在尝试从一个活动A转移到另一个活动B.在活动B中有一个回收站视图,在该回收站视图中,我在SQLite数据库中的后台线程(使用AsyncTask)中加载数据。当我切换到活动时BI已经给出了一个进度条来显示数据的加载。但是进度条不会旋转直到它完全加载数据。我希望它旋转以显示加载正在发生。

This some what looks like this,

When loading happens then it starts to spin,and looks like this.Running behind the recycler view.

package com.freakydevs.kolkatalocal;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import java.util.ArrayList;
import java.util.List;
public class TrainTable extends AppCompatActivity {
String from, to;
ProgressDialog progressDialog;
Toolbar toolbar;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.train_table);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    progressDialog = new ProgressDialog(this, ProgressDialog.THEME_HOLO_DARK);
    progressDialog.setMessage("Loading...");
    progressDialog.show();
}

@Override
protected void onStart() {
    super.onStart();
    new MyAsyncTask().execute();
}

public class MyAsyncTask extends AsyncTask<Void, Void, Cursor> {
    public int FromStationId, ToStationId;
    String sday, sday1, sday2, sday3;
    int check = 5;
    private List<Train> trainList = new ArrayList<>();
    private RecyclerView recyclerView;
    private TrainTableAdapter trainTableAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onPreExecute() {


        Intent intent = getIntent();
        sday = intent.getStringExtra("day");

        from = intent.getStringExtra("From");
        to = intent.getStringExtra("To");
        FromStationId = intent.getIntExtra("trainId", 0);
        ToStationId = intent.getIntExtra("trainId1", 0);
        check = intent.getIntExtra("samay", 0);


    }


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

       //Some codes are here to make a cursor and send it to postExecute
        c3 = SQ.rawQuery(s, null);
        return c3;

    }

    @Override
    protected void onPostExecute(final Cursor cursor) {



        if (cursor.getCount() == 0) {
            progressDialog.dismiss();
            setProgressBarIndeterminateVisibility(false);
            ShowNoTrain();


        } else {



            recyclerView = (RecyclerView) findViewById(R.id.tiimetable_list);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    trainTableAdapter = new TrainTableAdapter(TrainTable.this, cursor, from, to);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mLayoutManager = new LinearLayoutManager(TrainTable.this);
                            recyclerView.setLayoutManager(mLayoutManager);
                            recyclerView.setItemAnimator(new DefaultItemAnimator());
                            recyclerView.setAdapter(trainTableAdapter);
                            trainTableAdapter.notifyDataSetChanged();
                            progressDialog.dismiss();
                            setProgressBarIndeterminateVisibility(false);
                        }
                    });
                }
            }).start();


        }

    }
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == android.R.id.home) {
        this.finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
    super.onResume();
}

public void ShowNoTrain() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(TrainTable.this);

    // set title
    //alertDialogBuilder.setTitle("Invalid Date");

    // set dialog message
    alertDialogBuilder
            .setMessage("No Train Available for this Route!")
            .setCancelable(false);

    //final AlertDialog my = alertDialogBuilder.create();
    alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            finish();
        }
    });
    AlertDialog alertDialog = alertDialogBuilder.create();
    // create alert dialog


    // show it
    alertDialog.show();
}


}

0 个答案:

没有答案