Android - 自定义数组适配器在进出片段期间不调用

时间:2013-07-24 11:16:37

标签: java android android-fragments

现在已经有了这个问题,我有LaunchpadSectionFragment作为片段A,在onClick事件上另一个片段(B)被称为VideoPlayerFragment正在调用和显示扩展ListFragment

问题:

当我单击Fragment 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 个答案:

答案 0 :(得分:0)

感谢@Luksprog,给我提示。为了确保在ListFragment内调用自定义数组适配器,我必须将MySimpleArrayAdapter放在我的onPostExecute() AsyncTask内,以确保它将被执行在doBackground()

之后