我设法将一些数据从片段传递到对话框片段,我使用bundle来实现传递数据功能。但问题是我在对话框片段中保持获取空值,即使我调试程序已经显示我的包已包含数据。是不是我可以使用bundle来传递数据,或者我使用bundle的方式是错误的。
MainActivity
public class MainActivity extends Fragment {
Button test;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View h = inflater.inflate(R.layout.activity_main, container, false);
test = (Button) h.findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle args = new Bundle();
args.putString("key", "asdasdasdasd");
FragmentDialog newFragment = new FragmentDialog();
newFragment.setArguments(args);
newFragment.show(getActivity().getSupportFragmentManager(), "TAG");
}
});
onOpenDialog();
return h;
}
public void onOpenDialog()
{
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentDialog overlay = new FragmentDialog();
overlay.show(fm, "FragmentDialog");
}
}
FragmentDialog
public class FragmentDialog extends DialogFragment
{
private SectionsPagerAdapter sectionsPagerAdapter;
private ViewPager viewPager;
TextView textView;
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState)
{
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dialog, container);
// tab slider
sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
// Set up the ViewPager with the sections adapter.
viewPager = (ViewPager)view.findViewById(R.id.pager);
viewPager.setAdapter(sectionsPagerAdapter);
textView =(TextView)view.findViewById(R.id.textview);
Bundle mArgs = getArguments();
String myValue = mArgs.getString("key");
textView.setText(myValue);
return view;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0)
{
// find first fragment...
Fragment_Tab_1 ft1 = new Fragment_Tab_1();
return ft1;
}
if (position == 1)
{
// find first fragment...
Fragment_Tab_2 ft2 = new Fragment_Tab_2();
return ft2;
}
else if (position == 2)
{
// find first fragment...
Fragment_Tab_3 ft3 = new Fragment_Tab_3();
return ft3;
}
return null;
}
@Override
public int getCount() {
// Show 2 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "First Tab";
case 1:
return "Second Tab";
case 2:
return "Third Tab";
}
return null;
}
}
}
答案 0 :(得分:1)
试试这个:
在您的MainActivity中:
FragmentTransaction transection=getFragmentManager().beginTransaction();
FragmentDialog mfragment=new FragmentDialog ();
//using Bundle to send data
Bundle bundle=new Bundle();
bundle.putString("key","asdasdasdasd");
mfragment.setArguments(bundle); //data being send to SecondFragment
transection.replace(R.id.frag, mfragment);
transection.commit();
在FragmentDialog的onCreateView中:
String myStr = getArguments().getString("key");
text.setText(myStr);
答案 1 :(得分:1)
你可以使用newInstance静态方法:
public static FragmentDialog newInstance(int someInt) {
FragmentDialog yourFragmentDialog = new FragmentDialog();
Bundle args = new Bundle();
args.putInt("intVariable", someInt);
args.putString("stringVariable", someString);
yourFragmentDialog.setArguments(args);
return yourFragmentDialog;
}
然后获取数据:
getArguments().getInt("intVariable", 0);
希望它有所帮助:)
答案 2 :(得分:0)
请试试这个
public void onOpenDialog()
{
Bundle args = new Bundle();
args.putString("key", "KEY_FROM_OPEN_DIALOG");
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentDialog overlay = new FragmentDialog();
overlay.setArguments(args);
overlay.show(fm, "FragmentDialog");
}