从json获取数据一次,并在3个片段中使用它 [英] Getting data from json once and use it in 3 fragment
问题描述
我正在使用截击从JSON
获取数据,在Activity
中有3个Fragment
,我必须用从MainActivity
中从JSON
接收到的数据填充这些片段.我想一次从JSON
获取数据,并在Activity
和所有这3个Fragment
中使用它.我将接收到的数据放在这样的List
中:
I am getting the data from JSON
using volley, there is 3 Fragment
in Activity
which I have to populate these fragments with the data that I have received from JSON
in MainActivity
. I would like to get data from JSON
once and use it in Activity
and in all those 3 Fragment
. I put the received data in a List
like this:
List<Display_Activity_Model> videoDetailList;
然后我使用以下接口将此列表从活动发送到其他片段:
and I send this list from activity to other fragments using an Interface like:
在活动中,我有这种方法:
In activity I have this method:
@Override
public List<Display_Activity_Model> getDataList() {
return videoDetailList;
}
在其中一个片段中,我有:
In one of fragments I have:
public interface GetDataInterface {
List<Display_Activity_Model> getDataList();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
sGetDataInterface= (GetDataInterface) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "must implement GetDataInterface Interface");
}
}
@Override
public void onResume() {
super.onResume();
if(sGetDataInterface != null){
dataListFromDispAct = sGetDataInterface.getDataList();
}
}
当我调试代码时,我看到MainActivity
中的getDataList
方法在使用凌空获取json的方法之前被调用.因此,我一直都收到空白清单.
When I debug the code, I see that the method getDataList
in MainActivity
is called before the method for fetching json with volley. So all the time I receive empty list.
我的问题是:用齐射一次获取数据并将其用于其他片段的最佳方法是什么?
My question is that: What is the best way to fetch data with volley once and use it in other fragments?
Update1:
Update1:
@Keita Junichiro:
@Keita Junichiro:
我在片段中定义了更新"方法,例如:
I defined "update" method in fragment like:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.video_info,container,false);
initComponents(view);
if (dataListFromDispAct != null) {
txt_view_count.setText(dataListFromDispAct.get(0).getView_count());
}
return view;
}
public void update(List<Display_Activity_Model> videoDetailList){
dataListFromDispAct=videoDetailList;
}
并且在json加载到"getItemMenuGson"方法中后,我正在 activity 中调用"update"方法:
and I am calling "update" method in activity after json loaded in "getItemMenuGson" method:
getItemMenuGson(new CallBack() {
@Override
public void onSuccess(List<Display_Activity_Model> itemMenuGsonList) {
videoDetailList=itemMenuGsonList;
new Video_Info().update(videoDetailList);
}
@Override
public void onFail(String msg) {
}
});
问题是在更新方法之前调用了片段中的方法"onCreateView".当我声明更新方法为静态时,它也是相同的,并且变量"dataListFromDispAct"一直为空.
The problem is method "onCreateView" in fragment is called before update method. When I declare update method static it is also the same and variable "dataListFromDispAct" is null all the time.
Update2:
更新3: @Piyush
Update 3: @Piyush
我实施了您的回复,但此问题无法解决.我得到的是空列表,因为onActivity的onCreateView方法早于Activity中的getItemMenuGson方法执行.我宣布:
I implemented your respond, but it is not working in this problem. I am getting empty list because method onCreateView executing earlier than getItemMenuGson method in Activity. I declared :
SharedApplication mapp = SharedApplication.getInstance();
ArrayList<String> myList = mapp.getArrayListData();
在onActivityCreate中的
在片段中创建,但它也在Activity中的getItemMenuGson方法之前运行.方法的调用顺序:
in onActivityCreated in fragment but it is also running before getItemMenuGson method in Activity. The order in which methods are calling:
我该怎么办?如何在Activity中调用getItemMenuGson方法以获取JSON,然后加载片段布局以将那些已加载的数据填充到片段中?
what should I do? How can I call getItemMenuGson method in Activity to fetch JSON and then load fragment layout to populate those loaded data to the fragment?
解决方案:问题的关键是首先运行的静态方法:
Solution: The key for the problem was Static method which runs first:
在片段中定义以下代码:
public class Video_Info extends Fragment {
static TextView txt_view_count;
List<Display_Activity_Model> dataListFromDispAct;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.video_info,container,false);
txt_view_count= (TextView) view.findViewById(R.id.txt_view_count);
return view;
}
public static void update(List<Display_Activity_Model> videoDetailList)
{
txt_view_count.setText(videoDetailList.get(0).getView_count());
}
}
从JSON提取数据以列出调用更新方法后,在活动中进行
In Activity after fetching data from JSON to list call update method:
getItemMenuGson(new CallBack() {
@Override
public void onSuccess(List<Display_Activity_Model> itemMenuGsonList) {
videoDetailList = itemMenuGsonList;
//////////////////
Video_Info.update(videoDetailList);
///////////////////
Uri vidUri = Uri.parse(itemMenuGsonList.get(0).getMedia().getURL());
vidView.setVideoURI(vidUri);
}
推荐答案
为了在调用API之后将数据从活动发送到片段,您可以在片段中创建方法update()来接收数据并调用fragment.update()当loadData finish()处于活动状态时.
In order to send data from activity to fragment after call API, you can create a method update() in fragment to receive data and call fragment.update() in activity when loadData finish().
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Parse resonse to *videoDetailList*
fragment.update(videoDetailList);
}
}
这篇关于从json获取数据一次,并在3个片段中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!