2013-07-24 29 views
0

現在有了這個問題幾天了,我有LaunchpadSectionFragment作爲片段A,onClick事件另一個類的另一片段(B)被調用正在調用並顯示名爲VideoPlayerFragment的延伸ListFragmentAndroid - 自定義陣列適配器在進出片段時不被調用

問題:

當我點擊片段A將打開片段B,B片段中包含一個ListView - 這是由LoadAllProducts從數據庫填充,然後在onCreateView()調用加載返回的記錄的自定義陣列適配器在listView中。現在有時片段B顯示一個空白屏幕,如果我將返回到前一個片段(A)然後返回到B,它將顯示ListView。如果幸運的話,那麼第一次Frag B加載時,它會顯示listView。我一直在爲此而苦苦掙扎。感謝您的考慮。

public class CollectionDemoActivity extends FragmentActivity { 

    public static class DemoObjectFragment extends Fragment { 

     public static class LaunchpadSectionFragment extends ListFragment { 

        //onClick event opens another class which 
        //Extends another Fragment List 

        FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
        transaction.replace(R.id.contentFragment, fragment1, fragMainGroups); 
        transaction.addToBackStack(fragMainGroups); 
        transaction.commit(); 

     } 
    } 
} 

VideoPlayerFragment.java

public class VideoPlayerFragment extends ListFragment { 

    public void onCreate(Bundle e){ 
       super.onCreate(e); 

       productsList = new ArrayList<HashMap<String, ?>>(); 

       db = new DatabaseHandler(getActivity().getBaseContext()); 
       mContext = getActivity(); 
       Log.d("Inside", "onCreate"); 

       //this task will populate to get Data from database 
       //I checked in logcat and there are rows returned. 
       LoadAllProducts task = new LoadAllProducts(); 
        task.execute(); 


     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 

     //This custom array adapter is not sometimes called, sometimes not 
     //It really happens, I not sure with the reason. I also put some displays 
     //logs and sometimes it will execute the code, sometimes does not. 

     MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(
       mContext, productsList,R.layout.load_groups_activity_listview, FolderNameGroups, selectedId);   
       setListAdapter(adapter); 

     } 

}

+1

後執行程序使用一個'AsyncTask'得到數據可以安全地假設在'onCreateview()'完成之前它可能不會返回數據,這將使適配器依賴於一個空的'productsList'。當任務完成時,在'onPostExecute()'你做什麼? – Luksprog

+0

是啊!謝謝。讓我意識到,像我一樣在'onCreateView'上調用我的自定義arrray適配器並不重要。所以我做了什麼,我把我的適配器轉移到'onPostExecute()'自定義數組應該被調用。 – rahstame

回答

0

感謝@Luksprog,給我的提示。爲了確保自定義數組適配器將ListFragment內部調用,我已經把我的MySimpleArrayAdapteronPostExecute()AsyncTask以確保它將doBackground()

相關問題