[英]Pass Data from Activity to Fragment in Viewpager Tablayout
I used below code to passing data from Activity to Fragment.我使用下面的代码将数据从 Activity 传递到 Fragment。
Fragment Code片段代码
public class StoryDetailFragmentInfo extends Fragment {
View view;
TextView txtDescrible;
final static String getData = "abc";
public static StoryDetailFragmentInfo newInstance(String paramater){
Bundle bundle = new Bundle();
bundle.putString(getData,"Message");
StoryDetailFragmentInfo fragmentInfo = new StoryDetailFragmentInfo();
fragmentInfo.setArguments(bundle);
return fragmentInfo;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getArguments()!=null){
String test = getArguments().getString(getData);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.story_detail_fragment_info_layout, container, false);
txtDescrible = view.findViewById(R.id.txt_describleDetail);
return view;
}
} }
MainActivity Code主要活动代码
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.story_detail_layout);
addControls();
loadData();
intentData();
fillDataToView();
}
private void intentData() {
StoryDetailFragmentInfo.newInstance("Hello babe");
}
Problem is getArgument()
in my fragment is null
and i can't get data What should i do?问题是我的片段中的
getArgument()
为null
,我无法获取数据我该怎么办?
Can you help me explain it?你能帮我解释一下吗? Thanks for reading.
谢谢阅读。
If you want to pass data from activity to fragment as initial data, you can send data from activity to ViewPagerAdapter, then pass data from ViewPagerAdapter to fragment in getItem() function.如果要将数据从活动传递到片段作为初始数据,可以将数据从活动发送到 ViewPagerAdapter,然后在 getItem() 函数中将数据从 ViewPagerAdapter 传递到片段。 In Activity:
在活动中:
adapter = new ViewPagerAdapter(getSupportFragmentManager(), this,false)); adapter = new ViewPagerAdapter(getSupportFragmentManager(), this,false)); adapter.setData(data);
适配器.setData(数据);
In ViewPagerAdapter:在 ViewPagerAdapter 中:
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
MyFragment fragment = new MyFragment();
fragment.setData(data);
return fragment
}
}
Firstly, in the method newInstance
of class StoryDetailFragmentInfo
, you write these: bundle.putString(getData,"Message");
首先,在类
StoryDetailFragmentInfo
newInstance
方法中,你写这些: bundle.putString(getData,"Message");
while leave paramater
alone.而不要管
paramater
。 So you need change所以你需要改变
bundle.putString(getData,"Message");
to到
bundle.putString(getData,paramater);
Secondly, you try to get values from arguments from fragment by the key 'AdapterStoryDetailViewPager.idData', but this key is different from the key which you used to save value.其次,您尝试通过键“AdapterStoryDetailViewPager.idData”从片段的参数中获取值,但此键与您用于保存值的键不同。 So you need to change
所以你需要改变
String test = getArguments().getString(AdapterStoryDetailViewPager.idData);
to到
String test = getArguments().getString(getData);
After these two steps, you would get the right value.经过这两个步骤,您将获得正确的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.