Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-在片段进出期间未调用自定义数组适配器_Java_Android_Android Fragments - Fatal编程技术网

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

Java Android-在片段进出期间未调用自定义数组适配器,java,android,android-fragments,Java,Android,Android Fragments,几天来我一直在思考这个问题,我将LaunchpadSectionFragment作为片段a,在onClick事件中,另一个来自单独类的片段(B)被称为VideoPlayerFragment,它扩展了ListFragment,并被调用和显示 问题是: 当我单击片段A时,它会打开片段B,在片段B中包含一个listView,该listView由数据库中的LoadAllProducts填充,然后在onCreateView()中调用一个自定义数组适配器,在listView中加载返回的记录。现在有时候片段B

几天来我一直在思考这个问题,我将
LaunchpadSectionFragment
作为片段a,在onClick事件中,另一个来自单独类的片段(
B
)被称为
VideoPlayerFragment
,它扩展了
ListFragment
,并被调用和显示

问题是:

当我单击片段A时,它会打开片段B,在片段B中包含一个listView,该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);

       }
公共类VideoPlayerFragment扩展了ListFragment{
创建时的公共void(Bundle e){
super.onCreate(e);
productsList=新的ArrayList();
db=新的DatabaseHandler(getActivity().getBaseContext());
mContext=getActivity();
Log.d(“内部”、“onCreate”);
//此任务将填充以从数据库获取数据
//我签入了logcat,返回了行。
LoadAllProducts任务=新建LoadAllProducts();
task.execute();
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//此自定义阵列适配器有时不被调用,有时不被调用
//这真的发生了,我不确定原因。我还放了一些显示器
//日志,有时执行代码,有时不执行。
mySimpleArrayaAdapter=新的mySimpleArrayaAdapter(
mContext、productsList、R.layout.load\u groups\u activity\u listview、FolderNameGroups、selectedId);
setListAdapter(适配器);
}

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

当您使用
异步任务
获取数据时,可以安全地假设在
onCreateview()完成之前它可能不会返回数据,这将使适配器依赖于空的
产品列表
。当任务完成时,在
onPostExecute()
中,您会做什么?是的!非常感谢。让我意识到,在
onCreateView
上调用我的自定义arrray适配器并不重要。因此,我将我的适配器转移到
onPostExecute()
,应该调用该自定义数组。