2016-07-04 28 views
0

喜對不起,如果這是初學者的東西,但即時通訊使用嵌套的片段,所以我不能使用setretaininstance作爲即時通訊旋轉時真的掙扎在我的片段使用的AsyncTask有一個進度條,特別是(真),我嘗試以下this快速教程這就造成了我的方法如下使用的AsyncTask的片段加載與進步的數據庫條

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

和IM只是想引用它的片段連接後我想, 我有很多片段都需要使用相同的佈局只是用不同的查詢同一數據庫中的所有加載這種變化,所以是最好把自己的異步和進度條靜態方法,還是應該是獨一無二的每個片段,以及如何處理該取向對嵌套片段

任何人都可以指向我在這個正確的方向,或者可能指出我失蹤的東西

回答

1

最正確的方法是使用片段來保留異步任務的實例,通過旋轉。

這裏是一個鏈接非常簡單example使其易於遵循這一技術整合到自己的應用。

+0

它的一個不錯的博客帖子比你沒有提到嵌套片段一次笑,但環顧四周,我認爲這將真正幫助,多謝你 –

+0

你是惠康。 – Stanojkovic

相關問題