【问题标题】:how to pass a value using android navigation if the default value null如果默认值为null,如何使用android导航传递值
【发布时间】:2021-10-02 12:41:44
【问题描述】:

我正在使用带有安全参数的 android 导航组件。 我设置参数可以为空,默认值也为空。

问题是当我想传递任何值时会显示错误:

required: no arguments
found: Character
reason: actual and formal argument lists differ in length

我的片段 XML 代码:

<fragment
        android:id="@+id/bookListFragment"
        android:name="com.example.bookstory.UI.Fragments.BookListFragment"
        android:label="fragment_book_list"
        tools:layout="@layout/fragment_book_list">
            <argument
            android:name="character"
            app:argType="com.example.bookstory.DAO.Character"
            app:nullable="true"
            android:defaultValue="@null" />
</fragment>

我的行动:

      <action
            android:id="@+id/action_bookDescriptionFragment_to_bookListFragment"
            app:destination="@id/bookListFragment"
            app:popUpTo="@id/bookListFragment"
            app:popUpToInclusive="true" />

我不明白问题出在哪里 - 当我删除默认值时就可以了。

【问题讨论】:

  • 你如何传递值?..你没有包括那个
  • @Zain ``` ClassNameDirections.ActionName action = ClassNameDirections.actionName(character); Navigation.findNavController(v).navigate(action); ```

标签: android navigation android-safe-args


【解决方案1】:

ClassNameDirections.ActionName action = ClassNameDirections.actionName(character); Navigation.findNavController(v).navigate(action);

因为您的操作 ID 是 action_bookDescriptionFragment_to_bookListFragment

  <action
        android:id="@+id/action_bookDescriptionFragment_to_bookListFragment"

然后可以在this navigate()方法版本中使用NavDirections arg:

findNavController().navigate(BookListFragmentDirections.actionBookDescrioptionFragmentToBookListFragment()

这不会传入一个值,而是这样做:

因为你的值被命名为character:

 <argument
        android:name="character"
        app:argType="com.example.bookstory.DAO.Character"
        app:nullable="true"
        android:defaultValue="@null" />

然后您可以使用 safeArgs 生成的setCharacter() 方法级联操作:

findNavController().navigate(BookListFragmentDirections.actionBookDescrioptionFragmentToBookListFragment()
                    .setCharacter("c")

我看到您使用了 safeArgs,但以防万一您不想使用它;您可以使用this navigate() 方法版本通过捆绑对象设置值

【讨论】:

    【解决方案2】:

    我不了解 safeArgs,但如果您使用 safeArgs 没有得到答案,那么您可以使用普通捆绑包做到这一点。

    如果您不知道,您可以将包含您的数据的包作为第二个参数发送到该方法的导航操作。
    像这样

    char char_value = 'a' ;
    Bundle data = new Bundle() ;
    data.putChar("key",char_value);
    NavHostFragment.findNavController(this)
                        .navigate(R.id.action_bookDescriptionFragment_to_bookListFragment,data);
    

    在 booklistFragment 中,您只需在片段内的任何位置调用 getArguments() 即可获得这样的包

    public class BookListFragment extends Fragment {
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
        Bundle data = getArguments() ;
        char receivedChar = data.getChar("key",'z'); // 'z' is a default value incase it didn't find the key you're looking for it returns 'z'
        }
    

    您可以在其中放入各种东西,只需输入“放置”,然后查看自动完成的建议。

    如果使用捆绑包,则无需在 nav_graph xml 中添加 argument 部分。

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2015-11-12
      • 2013-04-12
      • 2018-06-20
      • 2013-12-08
      • 2021-04-19
      • 2023-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多