Passing data using navigation component – How come I need a values for the constructor when the guide doesn't?

huangapple go评论74阅读模式
英文:

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);
}

然而,当我试图按照这个指南进行操作时,它坚持要求我传递我只想设置的元素:

Passing data using navigation component – How come I need a values for the constructor when the guide doesn't?

这是在nav_graph.xml中相关的部分:

目标:

Passing data using navigation component – How come I need a values for the constructor when the guide doesn't?

操作:

Passing data using navigation component – How come I need a values for the constructor when the guide doesn't?

我之所以提问,是因为即使只将它们作为参数传递,或者将它们作为参数传递并进行设置,程序都声称没有接收到其中的一个元素。

英文:

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:

Passing data using navigation component – How come I need a values for the constructor when the guide doesn't?

Here is the parts relevant in the nav_graph.xml:

Destination:

Passing data using navigation component – How come I need a values for the constructor when the guide doesn't?

Action:

Passing data using navigation component – How come I need a values for the constructor when the guide doesn't?

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)

huangapple
  • 本文由 发表于 2020年4月9日 18:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61119434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定