将包从 Main Activity 发送到 Fragment
Sending Bundle from Main Activity to Fragment
我已经按照 google 教程进行操作,在我的代码中没有发现任何错误,但由于某种原因,我的包中的数据似乎不是从我的 Main Activity 发送的到我的片段(我的应用程序一直返回“0.00”)。请帮忙。
主要ACTIVITY:
@Override
public void assignButtonBWHW1Clicked(String assignButtonBWHWClicked) {
if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {
assignFragment1 = new assignFragment1();
buttonsGeneric = new genericBackNextFragment();
splitItFragmentManager3 = getFragmentManager();
splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);
splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
splitItFragmentTransaction3.addToBackStack(null);
splitItFragmentTransaction3.commit();
Bundle bundleAssign = new Bundle();
bundleAssign.putDouble("price1", 22);
assignFragment1.setArguments(bundleAssign);
}
}
片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
assignFragment1View = inflater.inflate(R.layout.assign_fragment_1, container, false);
item1 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_1);
item2 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_2);
priceItem1 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_1);
priceItem2 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_2);
Bundle bundleAssign = this.getArguments();
Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
priceItem1.setText(String.valueOf(price1Double));
return assignFragment1View;
}
尝试
Bundle bundleAssign = getArguments();
或者,
Bundle bundleAssign = getActivity().getArguments()
而不是
Bundle bundleAssign = this.getArguments();
像
一样获得价值
double result = bundleAssign.getDouble("price1");
尝试在获得片段管理器之前将包初始化放置到片段中。
if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {
assignFragment1 = new assignFragment1();
buttonsGeneric = new genericBackNextFragment();
Bundle bundleAssign = new Bundle();
bundleAssign.putDouble("price1", 22);
assignFragment1.setArguments(bundleAssign);
splitItFragmentManager3 = getFragmentManager();
splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);
splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
splitItFragmentTransaction3.addToBackStack(null);
splitItFragmentTransaction3.commit();
}
只需将 fragment
一侧的 key
从 priceItem1
更新为 price1
。
Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
到
Double price1Double = bundleAssign.getDouble("price1", 0.00);
让事情发生 clear.this 是因为您使用单独的密钥,同时将数据添加到捆绑包并使用不同的密钥进行检索。所以它返回 default value
即 0.00
那个 priceItem1
key
您必须在代码中进行两项更改。
第一项是在交易前设置参数
if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {
assignFragment1 = new assignFragment1();
Bundle bundleAssign = new Bundle();
bundleAssign.putDouble("price1", 22);
assignFragment1.setArguments(bundleAssign);
buttonsGeneric = new genericBackNextFragment();
splitItFragmentManager3 = getFragmentManager();
splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);
splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
splitItFragmentTransaction3.addToBackStack(null);
splitItFragmentTransaction3.commit();
}
第二个始终使用相同的密钥来设置和检索包中的数据,因此请更改片段
Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
至
Double price1Double = bundleAssign.getDouble("price1", 0.00);
我已经按照 google 教程进行操作,在我的代码中没有发现任何错误,但由于某种原因,我的包中的数据似乎不是从我的 Main Activity 发送的到我的片段(我的应用程序一直返回“0.00”)。请帮忙。
主要ACTIVITY:
@Override
public void assignButtonBWHW1Clicked(String assignButtonBWHWClicked) {
if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {
assignFragment1 = new assignFragment1();
buttonsGeneric = new genericBackNextFragment();
splitItFragmentManager3 = getFragmentManager();
splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);
splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
splitItFragmentTransaction3.addToBackStack(null);
splitItFragmentTransaction3.commit();
Bundle bundleAssign = new Bundle();
bundleAssign.putDouble("price1", 22);
assignFragment1.setArguments(bundleAssign);
}
}
片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
assignFragment1View = inflater.inflate(R.layout.assign_fragment_1, container, false);
item1 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_1);
item2 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_2);
priceItem1 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_1);
priceItem2 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_2);
Bundle bundleAssign = this.getArguments();
Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
priceItem1.setText(String.valueOf(price1Double));
return assignFragment1View;
}
尝试
Bundle bundleAssign = getArguments();
或者,
Bundle bundleAssign = getActivity().getArguments()
而不是
Bundle bundleAssign = this.getArguments();
像
一样获得价值double result = bundleAssign.getDouble("price1");
尝试在获得片段管理器之前将包初始化放置到片段中。
if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {
assignFragment1 = new assignFragment1();
buttonsGeneric = new genericBackNextFragment();
Bundle bundleAssign = new Bundle();
bundleAssign.putDouble("price1", 22);
assignFragment1.setArguments(bundleAssign);
splitItFragmentManager3 = getFragmentManager();
splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);
splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
splitItFragmentTransaction3.addToBackStack(null);
splitItFragmentTransaction3.commit();
}
只需将 fragment
一侧的 key
从 priceItem1
更新为 price1
。
Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
到
Double price1Double = bundleAssign.getDouble("price1", 0.00);
让事情发生 clear.this 是因为您使用单独的密钥,同时将数据添加到捆绑包并使用不同的密钥进行检索。所以它返回 default value
即 0.00
那个 priceItem1
key
您必须在代码中进行两项更改。
第一项是在交易前设置参数
if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {
assignFragment1 = new assignFragment1();
Bundle bundleAssign = new Bundle();
bundleAssign.putDouble("price1", 22);
assignFragment1.setArguments(bundleAssign);
buttonsGeneric = new genericBackNextFragment();
splitItFragmentManager3 = getFragmentManager();
splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);
splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
splitItFragmentTransaction3.addToBackStack(null);
splitItFragmentTransaction3.commit();
}
第二个始终使用相同的密钥来设置和检索包中的数据,因此请更改片段
Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
至
Double price1Double = bundleAssign.getDouble("price1", 0.00);