[英]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.