在片段中使用asynctask来加载带有进度条的数据库

时间:2016-07-04 21:28:56

标签: android android-fragments android-asynctask

对不起,如果这是初学者的东西,但我真的很难在我的片段中使用asynctask进度条,特别是当我使用嵌套片段旋转,所以我不能使用setretaininstance(true),我尝试跟随this快速教程导致了我的方法

public class OneFragment extends fragment {
private WeakReference<MyAsyncTask> asyncTaskWeakRef;
}

private void startNewAsyncTask() {
    MyAsyncTask asyncTask = new MyAsyncTask(this);
    this.asyncTaskWeakRef = new WeakReference<MyAsyncTask >(asyncTask );
    asyncTask.execute();
}

public void showProgressBar() {
    ProgressBar progress = (ProgressBar)getActivity().findViewById(R.id.progressBarFetch);
    progress.setVisibility(View.VISIBLE);
    progress.setIndeterminate(true);
}

public void hideProgressBar() {
    ProgressBar progress = (ProgressBar)getActivity().findViewById(R.id.progressBarFetch);
    progress.setVisibility(View.GONE);

}

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    private WeakReference<OneFragment> fragmentWeakRef;

    private MyAsyncTask (OneFragment fragment) {
        this.fragmentWeakRef = new WeakReference<OneFragment>(fragment);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showProgressBar();
    }

    @Override
    protected Void doInBackground(Void... params) {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getActivity(), "ADD_NEW_CARD", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        addNewCardDao leaseDao = daoSession.getAddNewCardDao();
        QueryBuilder qb = leaseDao.queryBuilder();
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getActivity());
        //  Create a new boolean and preference and set it to true
        prefString = sharedPreferences.getString("ICONS SELECTED","ORIGINAL");
        switch (prefString){
            case "ORIGINAL":

                leaseList.clear();
                String[] strings1 = {"ORIGINAL", "USER"};
                qb.where(addNewCardDao.Properties.CardIconType.in(strings1),
                        qb.and(addNewCardDao.Properties.Type_of_type.eq("PEOPLE"),
                                addNewCardDao.Properties.Type_of_word.eq("NOUNS")));
                leaseList = qb.list();
                db.close();
                break;
            case "SIMPLE":
                leaseList.clear();
                String[] strings2 = {"SIMPLE", "USER"};
                qb.where(addNewCardDao.Properties.CardIconType.in(strings2),
                        qb.and(addNewCardDao.Properties.Type_of_type.eq("PEOPLE"),
                                addNewCardDao.Properties.Type_of_word.eq("NOUNS")));
                leaseList = qb.list();
                db.close();
                break;
            case "PHOTOS":
                leaseList.clear();
                String[] strings3 = {"PHOTOS", "USER"};
                qb.where(addNewCardDao.Properties.CardIconType.in(strings3),
                        qb.and(addNewCardDao.Properties.Type_of_type.eq("PEOPLE"),
                                addNewCardDao.Properties.Type_of_word.eq("NOUNS")));
                leaseList = qb.list();
                db.close();
                break;
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void response) {
        super.onPostExecute(response);
        if (this.fragmentWeakRef.get() != null) {
            hideProgressBar();
            CardAdapterDB cardAdapterDB = new CardAdapterDB(leaseList);
            recyclerView.setAdapter(cardAdapterDB);
            cardAdapterDB.notifyDataSetChanged();
        }
    }
}

这似乎工作但旋转崩溃时进度条出现空指针错误,所以我尝试更改进度条查找视图getActivity()调用rootView,因为这就是我如何给我的视图充气

 View rootView = inflater.inflate(R.layout.fragment_one, container, false);

但这不起作用它再次使用空指针崩溃,但这次是在应用程序启动之前,但我的布局中肯定有这个进度条

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

我只想在片段附加后尝试引用它我认为, 我有许多片段都需要这个更改所有使用相同的布局,只使用不同的查询从同一个数据库加载,所以最好为异步和进度条创建一个静态方法,或者它们是否对每个片段是唯一的,并且我该如何处理嵌套片段的方向

任何人都可以指出我正确的方向,或者指出我缺少的东西

1 个答案:

答案 0 :(得分:1)

最合适的方法是使用片段来保留异步任务的实例,而不是旋转。

以下是非常简单example的链接,可让您轻松将此技术集成到您的应用中。