Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我怎样才能像原意一样将适配器传递给片段_Android_Android Fragments_Android Intent - Fatal编程技术网

Android 我怎样才能像原意一样将适配器传递给片段

Android 我怎样才能像原意一样将适配器传递给片段,android,android-fragments,android-intent,Android,Android Fragments,Android Intent,我是android新手,我尝试在点击后从适配器调用我的MapFragment,下面是我的代码 下面是适配器代码: public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { final BusInfo info = getItem(position); View view = LayoutInflater.from(context).inflate(R.layout.

我是android新手,我尝试在点击后从适配器调用我的
MapFragment
,下面是我的代码

下面是适配器代码:

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  final BusInfo info = getItem(position);
  View view = LayoutInflater.from(context).inflate(R.layout.bus_only_list,null);
  TextView busname;
  busname = (TextView) view.findViewById(R.id.busname);
  busname.setText(info.name);
  view.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       pref = context.getSharedPreferences("busInfo",Context.MODE_PRIVATE);
       SharedPreferences.Editor editor = pref.edit();
       editor.putString("bus_name",info.name);
       editor.commit();

       Intent intent = new Intent(context, MapsFragment.class);
       intent.putExtra("name",info.name);
       context.startActivity(intent);>     

     }
  });

  return view;
}
我想使用intent传递到mapfragment,但它重定向到
MainActivity
,而不是
mapfragment
。我如何停止转移到MainActivity

多谢各位

正如前面提到的: 1.在您的上下文中使用回调或强制转换(您的活动必须自行处理更改的片段)。 2.要更改片段,请使用活动的FragmentManager-意图用于启动另一个活动


不能将意图传递给片段。试着用一个包代替

Bundle bundle = new Bundle();

bundle.putString("name", info.name);

mapFragment.setArguments(bundle)
在片段(MapsFragment)中,按如下方式获取捆绑包:

Bundle bundle = this.getArguments();

if(bundle != null){
   String infoName = bundle.getString("name");
}

向片段传递值的常见模式是使用
newInstance
方法。在这个方法中,您可以将参数设置为fragment,作为发送值的方法

首先,创建
newInstance
方法:

public class YourFragment extends Fragment {
  ...

  // Creates a new fragment with bus_name
  public static YourFragment newInstance(String busName) {
    YourFragment yourFragment = new YourFragment();
    Bundle args = new Bundle();
    args.putString("bus_name", busName);
    yourFragment.setArguments(args);
    return yourFragment;
  }

  ...
}
然后,您可以在
onCreate
中获得值:

public class YourFragment extends Fragment {
  ...

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the value from arguments
    String busName = getArguments().getString("bus_name", "");  
  }

  ...
}
您可以使用以下命令将值设置为活动中的片段:

FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
YourFragment yourFragment = YourFragment.newInstance("bus_name_value");
fragTransaction.replace(R.id.fragment_place_holder, yourFragment);
fragTransaction.commit();
您可以使用上述代码在片段初始化中发送值

如果要将值设置为已实例化的片段,可以创建一个方法,然后调用该方法来设置值:

public class YourFragment extends Fragment {
  ...

  public setBusName(String busName) {
    // set the bus name to your fragment.
  }

  ...
}
现在,在活动中,您可以通过以下方式调用它:

// R.id.yourFragment is the id of fragment in xml
YourFragment yourFragment = (YourFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.yourFragment);
yourFragment.setBusName("bus_name_value");

您可以使用界面来完成此操作……您不能使用意图加载片段问题并不十分清楚,但如果您在片段A中,并且在单击之后,您希望在同一活动中转到片段B,onclick应该告诉MainActivity切换到片段B,不要直接执行任何涉及片段B的操作。MainActivity可以使用FragmentManager或SupportFragmentManager切换片段。