从 Fragment 类调用 Web 服务(API)的正确方法 [英] Right Approach to Call Web Service(API) from Fragment class

查看:31
本文介绍了从 Fragment 类调用 Web 服务(API)的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道,在哪个片段回调方法中,我们应该调用一个web服务,在返回片段web服务后不应再次调用.

I need to know, In which fragment callback method, we should call a web service by which after come back to fragment web service should not call again.

例如.我有一个片段类 MyFragment.java

For example. I have a fragment class MyFragment.java

public class MyFragment extends Fragment {


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

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

        return rootView;
    }

}

我需要知道我必须调用哪个回调方法来更新片段的 UI.现在我从 onCreateView 方法 调用网络服务.但我需要知道从片段调用 Web 服务的最佳方式是什么.

I need to know which callback method I have to call webservice to update the UI of fragment. Right Now I am calling web services from onCreateView method. but I need to know what should be best way to call web service from fragment.

推荐答案

如果我正确理解你的问题,你想从服务器获取一些数据,然后通知片段数据准备好并重新绘制片段,是否正确?根据文档这里:

If I understand your problem correctly, you want to fetch some data from the server and then inform the fragment that data is prepared and redraw the fragment, is that correct? According to the documentation here:

onCreate() -系统在创建片段时调用它.在您的实现中,您应该初始化要在片段暂停或停止然后恢复时保留的片段的基本组件.

onCreate() - The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

onCreateView()当片段第一次绘制其用户界面时,系统会调用它.要为片段绘制 UI,必须从此方法返回一个视图,该视图是片段布局的根.如果片段不提供 UI,您可以返回 null.

onCreateView() The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

当您在应用程序的其他地方创建 Fragment 时,会调用 onCreate() 方法.当第一次需要绘制片段时,onCreateView() 被调用并且这个方法返回一个创建的视图.就您而言,您可能会采用以下方法:

When you create a Fragment somewhere else in your application, onCreate() method is called. When fragment has to be drawn for the first time, onCreateView() is called and this method returns a created View. In your case, you could probably go with something like:

  1. 为此数据和适配器(如果使用的话)声明一个实例变量(容器).
  2. onCreate中,初始化所有这些数据(空容器),初始化适配器,然后执行AsyncTask.
  3. onCreateView 中,准备返回的视图 - 适配器等.所以现在,一旦 AsyncTask 完成,在 onPostExecute 中它会调用 your_adapter.notifyDataSetChanged().这将重绘片段,因为适配器将被告知数据已更改(从服务器获取).
  1. Declare an instance variable (container) for this data and adapter (if you use any).
  2. In onCreate, initialize all this data (empty container), initialize adapter and then execute the AsyncTask.
  3. In onCreateView, prepare the view to return - adapter etc. So now, once AsyncTask will finish, in onPostExecute it calls your_adapter.notifyDataSetChanged(). This will redraw the fragment, since adapter will be informed that data has changed (fetched from the server).

这篇关于从 Fragment 类调用 Web 服务(API)的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆