[英]Android get the value null when passing from fragment to dialogfragment
i have a question i using bundle to send data from fragment to dialogfragment, but there is a problem when i click on the button in my fragment layout it success to get the data from fragment but in my dialogfragment it get the null value , when i debug i found that my dialog fragment run two time so that why i get the null value. 我有一个问题,我使用包将数据从片段发送到dialogfragment,但是当我单击片段布局中的按钮时,成功从片段中获取数据却在我的dialogfragment中获取空值是有问题的调试我发现我的对话框片段运行了两次,所以为什么我得到了空值。 Any suggestion to solve this problem, sorry for my english, hope can understand my question.
任何解决这个问题的建议,对不起我的英语,希望能理解我的问题。
This is my onclick to pass data to my dialog fragment 这是我将数据传递到我的对话框片段的onclick
Button[i].setBackgroundColor(0xFF00FF00);
Button[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView test = (TextView)getView().findViewById(R.id.testproductname);
test.setText(pName);
String testproductname = test.getText().toString();
Bundle args = new Bundle();
args.putString("key", testproductname);
FragmentDialog newFragment = new FragmentDialog();
newFragment.setArguments(args);
newFragment.show(getActivity().getSupportFragmentManager(), "TAG");
showTheDialog();
}
});
}
protected void showTheDialog() {
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentDialog overlay = new FragmentDialog();
overlay.show(fm, "FragmentDialog");
}
FragmentDialog 片段对话框
public class FragmentDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(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.prompt_quantity, container);
// tab slider
sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
// Set up the ViewPager with the sections adapter.
viewPager = (ViewPager) view.findViewById(R.id.modifierpager);
viewPager.setAdapter(sectionsPagerAdapter);
Bundle mArgs = getArguments();
pName = mArgs.getString("key","asdasd");
final TextView tpn = (TextView)getView().findViewById(R.id.gtestproductname);
tpn.setText(pName);
}
newFragment.show(getActivity().getSupportFragmentManager(), "TAG");
showTheDialog();
you need not call show twice, this logic draws the view twice. 您无需调用show两次,此逻辑两次绘制视图。 That is why you have the anomaly.
这就是为什么您有异常。 Remove the second one.
删除第二个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.