简体   繁体   English

如何将值从列表视图适配器传递到android中的片段

[英]how to pass values from List View Adapter to fragment in android

I want to pass same value from adapter to fragment and I send it in next way: 我想将相同的值从适配器传递到片段,然后以以下方式发送它:

Fragment fr = new Fragment 

fr.deleteMothod(Position);

when I call above code , it is creating new instance of Fragment therefore I am not accessing values of fragment. 当我调用上述代码时,它正在创建Fragment的新实例,因此我不访问fragment的值。 How to use old instance of fragment. 如何使用片段的旧实例。

You can modify the constructor of your fragment like so: 您可以像这样修改片段的构造函数:

 public static DetailsFragment newInstance(String selection, String[] selectionArgs) {
        DetailsFragment detailsFragment = new DetailsFragment();
        Bundle args = new Bundle();
        args.putString("selection", selection);
        args.putStringArray("selectionArgs", selectionArgs);
        detailsFragment.setArguments(args);
        return detailsFragment;
    }

However there is a much better way of doing this, if you use an interface. 但是,如果使用接口,则有更好的方法。

You should retrieve your fragment from the Fragment manager by id or tag, then call the method on it. 您应该通过ID或标签从“片段管理器”中检索您的片段,然后在其上调用方法。 These are the official docs http://developer.android.com/training/basics/fragments/communicating.html#Deliver 这些是官方文档http://developer.android.com/training/basics/fragments/communicating.html#Deliver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 java 将所选项目(我的 recyclerview 中的多个项目)值从适配器传递到 android 中的片段 - How to pass the selected items (multiple items in my recyclerview) values from the adapter to the fragment in android using java 如何将数据从片段传递到Android中的recyclerview适配器类 - How to pass data from fragment to recyclerview adapter class in android 如何在Android Studio中将数据从适配器传递到片段 - How to pass data from adapter to fragment in android studio android - 如何将侦听器从片段传递给适配器? (科特林) - android - How to pass listener to an Adapter from a Fragment? (Kotlin) 在Android片段中设置列表视图适配器 - set list view adapter in a fragment in android 从适配器到片段类的传递值有问题吗? - Trouble In pass values from adapter to fragment class? Android将值从片段传递到适配器类 - Android Pass Value from Fragment to Adapter Class 将值从回收站适配器传递到android中的片段 - passing values from recycler adapter to fragment in android 如何将数组列表从适配器传递到片段? - How to pass an arraylist to a fragment from adapter? 如何将数据从Recyclerview适配器传递到片段 - How to pass data from recyclerview adapter to fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM