正确方法从片段类调用Web服务(API) [英] Right Approach to Call Web Service(API) from Fragment class
问题描述
我需要知道,其中片段回调方法,我们应该调用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;
}
}
我需要知道我要调用webservice的更新片段的UI哪个回调方法。从 onCreateView
现在我调用Web服务方法。但我需要知道什么应该是从片段调用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.
推荐答案
如果我正确地理解你的问题,你想从服务器获取一些数据,然后通知该数据是prepared片段和重绘的片段,那是对的吗?据这里文档:
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,你必须从这种方法那是你的片段布局的根返回一个视图。你可以返回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.
当你在你的应用程序中创建一个片段别处,的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:
- 声明此数据和适配器一个实例变量(容器)(如果使用的话)。
- 在
的onCreate
,初始化所有数据(空箱),初始化适配器,然后执行的AsyncTask
。 - 在
onCreateView
,prepare视图返回 - 适配器等。所以,现在,一旦的AsyncTask
将说完,在onPostExecute
调用your_adapter.notifyDataSetChanged()
。这将重绘的片段,因为适配器将被告知数据已经改变(从服务器获得)。
- Declare an instance variable (container) for this data and adapter (if you use any).
- In
onCreate
, initialize all this data (empty container), initialize adapter and then execute theAsyncTask
. - In
onCreateView
, prepare the view to return - adapter etc. So now, onceAsyncTask
will finish, inonPostExecute
it callsyour_adapter.notifyDataSetChanged()
. This will redraw the fragment, since adapter will be informed that data has changed (fetched from the server).
这篇关于正确方法从片段类调用Web服务(API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!