Java 片段的Delay onCreateView()

Java 片段的Delay onCreateView(),java,android,android-fragments,Java,Android,Android Fragments,我的代码通过加载程序从internet获取数据,这些数据通过bundle传递到片段中,然后应该显示在片段的视图中 问题是,fragement的onCreateView()-方法get在我的数据准备好之前被调用,所以bundles参数为null 如何推迟该过程,以便在调用onCreateView()时数据准备就绪 我试着发布必要的代码,如果你还需要,请告诉我 最初,我试图将整个列表新闻条目传递给片段,但失败得很厉害,因此我不得不将其转换为StringArray,以便将其放入捆绑包中。如果你能告诉我

我的代码通过加载程序从internet获取数据,这些数据通过bundle传递到片段中,然后应该显示在片段的视图中

问题是,fragement的onCreateView()-方法get在我的数据准备好之前被调用,所以bundles参数为null

如何推迟该过程,以便在调用onCreateView()时数据准备就绪

我试着发布必要的代码,如果你还需要,请告诉我

最初,我试图将整个列表新闻条目传递给片段,但失败得很厉害,因此我不得不将其转换为StringArray,以便将其放入捆绑包中。如果你能告诉我一个更好的方法来将这个列表传递给片段,我也将非常感激:)

多谢各位

SpinnerActivity.java

@Override
public void onItemSelected(
        AdapterView<?> parent, 
        View view, 
        int position, 
        long id
) {
    mQuery = parent.getItemAtPosition(position).toString();
    buildQuery();
    loaderManager.destroyLoader(0);
    Loader loader = loaderManager.initLoader(0, null, this);
}

public void onLoadFinished(
        Loader<List<NewsEntry>> loader, 
        List<NewsEntry> fetchedNewsEntries
) {
    Bundle bundle = new Bundle();
    ArrayList<String> newsEntriesArrayList = new ArrayList<>();

    ...

    bundle.putStringArrayList("newsEntries", newsEntriesArrayList);
    NewsEntryFragment fragment = new NewsEntryFragment();
    fragment.setArguments(bundle);
}
public NewsEntryFragment() {}

public static void newInstance(int columnCount) {

    NewsEntryFragment fragment = new NewsEntryFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(
        @NonNull LayoutInflater inflater, 
        ViewGroup container,
        Bundle savedInstanceState
) {
    View view = inflater.inflate(R.layout.fragment_newsentry_list, container, false);

    if (getArguments() != null) {
        ArrayList<String> newsEntriesStringArrayList = 
            new ArrayList<>(
                Objects.requireNonNull( 
                    getArguments().getStringArrayList( "newsEntries" ) 
                )
            )
        ;

        ArrayList<NewsEntry> newsEntriesArrayList = new ArrayList<>();
            for (int i = 0; i < newsEntriesStringArrayList.size(); i++){
                StringBuilder builder = new StringBuilder();
                String sArray[];
                sArray = newsEntriesStringArrayList.get(i).split(",");
                NewsEntry newsEntry = 
                    new NewsEntry(
                        sArray[0], 
                        sArray[1], 
                        sArray[2], 
                        sArray[3], 
                        sArray[4]
                    )
                ;

                newsEntriesArrayList.add(newsEntry);
            }
    }


    // Set the adapter
    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;
        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
        }
        recyclerView
            .setAdapter(
                new MyNewsEntryRecyclerViewAdapter(
                    mNewsEntries, 
                    mListener
                )
            )
        ;
    }
    return view;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnListFragmentInteractionListener) {
        mListener = (OnListFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnListFragmentInteractionListener");
    }
}
@覆盖
公营部门选举(
AdapterView父级,
视图视图,
int位置,
长id
) {
mQuery=parent.getItemAtPosition(position.toString();
buildQuery();
loaderManager.destroyLoader(0);
Loader Loader=loaderManager.initLoader(0,null,this);
}
已完成公共无效加载(
装载机,
列出获取的新闻条目
) {
Bundle=新Bundle();
ArrayList newsEntriesArrayList=新的ArrayList();
...
bundle.putStringArrayList(“newsEntries”,newsEntriesArrayList);
NewEntryFragment=新的NewEntryFragment();
fragment.setArguments(bundle);
}
newentryfragment.java

@Override
public void onItemSelected(
        AdapterView<?> parent, 
        View view, 
        int position, 
        long id
) {
    mQuery = parent.getItemAtPosition(position).toString();
    buildQuery();
    loaderManager.destroyLoader(0);
    Loader loader = loaderManager.initLoader(0, null, this);
}

public void onLoadFinished(
        Loader<List<NewsEntry>> loader, 
        List<NewsEntry> fetchedNewsEntries
) {
    Bundle bundle = new Bundle();
    ArrayList<String> newsEntriesArrayList = new ArrayList<>();

    ...

    bundle.putStringArrayList("newsEntries", newsEntriesArrayList);
    NewsEntryFragment fragment = new NewsEntryFragment();
    fragment.setArguments(bundle);
}
public NewsEntryFragment() {}

public static void newInstance(int columnCount) {

    NewsEntryFragment fragment = new NewsEntryFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(
        @NonNull LayoutInflater inflater, 
        ViewGroup container,
        Bundle savedInstanceState
) {
    View view = inflater.inflate(R.layout.fragment_newsentry_list, container, false);

    if (getArguments() != null) {
        ArrayList<String> newsEntriesStringArrayList = 
            new ArrayList<>(
                Objects.requireNonNull( 
                    getArguments().getStringArrayList( "newsEntries" ) 
                )
            )
        ;

        ArrayList<NewsEntry> newsEntriesArrayList = new ArrayList<>();
            for (int i = 0; i < newsEntriesStringArrayList.size(); i++){
                StringBuilder builder = new StringBuilder();
                String sArray[];
                sArray = newsEntriesStringArrayList.get(i).split(",");
                NewsEntry newsEntry = 
                    new NewsEntry(
                        sArray[0], 
                        sArray[1], 
                        sArray[2], 
                        sArray[3], 
                        sArray[4]
                    )
                ;

                newsEntriesArrayList.add(newsEntry);
            }
    }


    // Set the adapter
    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;
        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
        }
        recyclerView
            .setAdapter(
                new MyNewsEntryRecyclerViewAdapter(
                    mNewsEntries, 
                    mListener
                )
            )
        ;
    }
    return view;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnListFragmentInteractionListener) {
        mListener = (OnListFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnListFragmentInteractionListener");
    }
}
public newentryfragment(){}
公共静态void newInstance(int columnCount){
NewEntryFragment=新的NewEntryFragment();
Bundle args=新Bundle();
args.putInt(ARG\u COLUMN\u COUNT,columnCount);
fragment.setArguments(args);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
mColumnCount=getArguments().getInt(ARG\u COLUMN\u COUNT);
}
}
@凌驾
公共视图onCreateView(
@非空充气机,
视图组容器,
捆绑存储状态
) {
视图=充气机。充气(R.layout.fragment\u newentry\u list,container,false);
如果(getArguments()!=null){
ArrayList NewsEntriessTringrayList=
新阵列列表(
对象。requireNonNull(
getArguments().getStringArrayList(“新闻条目”)
)
)
;
ArrayList newsEntriesArrayList=新的ArrayList();
对于(int i=0;iif(mColumnCount将用于获取数据的代码从onCreateView()移动到onViewCreated()
为了发送arrayList,您需要使NewsEntry类可包裹,然后您可以执行以下操作-

    args.putParcelableArrayList("list", yourList);

在您的片段中进行接口回调,并在任务完成后调用接口

我是否可以建议使用AsyncTasks加载数据并仅显示加载符号,直到数据准备就绪?使用空数组初始化recyclerview,当您的数据提取调用notifyDataSetChanged on Adapter时,我不会使用绑定来在活动和片段之间传递数据。在活动内部定义一个接口,让片段实现该接口。很遗憾,这没有帮助。问题是,在加载程序创建片段视图之前,就创建了该视图。我需要更改它。因此,我现在实现了该接口,我确实得到了响应来自internet查询的数据在我的片段中,但它仍然会在我的数据准备就绪之前触发接口。只需在后台线程中执行任务,如异步或服务。完成任务后,在任务执行代码的下一行调用接口