I'm trying to navigate from a RecyclerView adapter to a detail fragment, passing an id value.
I can navigate from an item selected in the RecyclerView to open the GameDetailFragment using the following:
this.cardGame.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NavDirections action = ScoreboardFragmentDirections.actionScoreboardFragmentToGameDetailFragment(); Navigation.findNavController(v).navigate(action); } });
However, I can't seem to find how to pass data when clicking on the CardView item to the GameDetailFragment to query the details.
I can find many examples in Kotlin, but I'm just learning Java and Android and would rather not start with Kotlin.
Using the example from the documentation:
@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); }
It seems it's inaccurate as the documentation indicates that the word "action" is added to the END of the class name.
I decided to look at the actual generated class file, without modifying it, of course. The word "action" is added to the BEGINNING of the class file name or combination of the originating and receiving destinations.
The result:
this.cardGame.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String strTemp = "todo"; ScoreboardFragmentDirections.ActionScoreboardFragmentToGameDetailFragment action = ScoreboardFragmentDirections.actionScoreboardFragmentToGameDetailFragment(); action.setMessage(strTemp); Navigation.findNavController(v).navigate(action); } });
P.S. Have you ever even tried to follow along with the documentation? There is very little, if any, flow to their descriptions.
One paragraph they're talking about cats & dogs, then the next paragraph they talk about apples and oranges. As a noobie/beginner, it's incredibly frustrating because somehow you have to figure out how they all interconnect/interrelate to one another.
Then, when we ask, we get snooty responses referring right back to the same documentation. Like really...
But, what do I know, I'm only a beginner.
https://stackoverflow.com/questions/66484821/android-java-navigate-to-fragment-passing-data-with-navigation-component March 05, 2021 at 08:01AM
没有评论:
发表评论