英文:
Passing data using navigation component - How come I need a values for the constructor when the guide doesn't?
问题
我正在使用此Android指南来在导航目标之间传递数据。
您可以在下面的代码片段中看到,该指南不需要将任何值传递给构造函数,而是设置这些值:
@Override
public void onClick(View view) {
EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
int amount = Integer.parseInt(amountTv.getText().toString());
ConfirmationAction action =
SpecifyAmountFragmentDirections.confirmationAction();
action.setAmount(amount);
Navigation.findNavController(view).navigate(action);
}
然而,当我试图按照这个指南进行操作时,它坚持要求我传递我只想设置的元素:
这是在nav_graph.xml中相关的部分:
目标:
操作:
我之所以提问,是因为即使只将它们作为参数传递,或者将它们作为参数传递并进行设置,程序都声称没有接收到其中的一个元素。
英文:
I am using this android guide for passing data between navigation destinations.
You can see in the code snippet below the guide doesn't need any values passed to the constructor, and it sets the values rather than passing them:
@Override
public void onClick(View view) {
EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
int amount = Integer.parseInt(amountTv.getText().toString());
ConfirmationAction action =
SpecifyAmountFragmentDirections.confirmationAction()
action.setAmount(amount)
Navigation.findNavController(view).navigate(action);
}
However, when I am trying to follow this, like the guide directs it insists I put pass elements which I want to only set:
Here is the parts relevant in the nav_graph.xml:
Destination:
Action:
I ask, because even only passing them as parameters or passing them as parameters and setting them as well the program claims it didn't receive one of the elements.
答案1
得分: 1
你可以使用这个来通过Navigation Architecture Component
传递数据(例如一个Bundle
)
EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
int amount = Integer.parseInt(amountTv.getText().toString());
Bundle bundle = new Bundle();
bundle.putInt("AMOUNT", amount);
findNavController().navigate(R.id.IdOfTheFragemntYouAreTryingToNavigate, bundle, null, null);
英文:
You can use this to pass data(i.e a Bundle
) using Navigation Architecture Component
EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
int amount = Integer.parseInt(amountTv.getText().toString());
Bundle bundle = new Bundle();
bundle.putInt("AMOUNT",amount);
findNavController().navigate(R.id.IdOfTheFragemntYouAreTryingToNavigate, bundle, null, null)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论