Android 如何将数据从一个片段发送到另一个片段?
嗨,我知道这个问题有答案。我已经尝试了所有这些,但在我的应用程序中不起作用。 我正在开发一个有3个片段活动的应用程序。第一个片段显示一个网站,第二个片段显示listview,第三个片段显示另一个listview。现在我想在用户单击listitem时将URL从第三个片段发送到第一个片段。这就是我所做的 我正在将字符串url从这个片段发送到第一个片段Android 如何将数据从一个片段发送到另一个片段?,android,android-fragments,android-listview,android-webview,Android,Android Fragments,Android Listview,Android Webview,嗨,我知道这个问题有答案。我已经尝试了所有这些,但在我的应用程序中不起作用。 我正在开发一个有3个片段活动的应用程序。第一个片段显示一个网站,第二个片段显示listview,第三个片段显示另一个listview。现在我想在用户单击listitem时将URL从第三个片段发送到第一个片段。这就是我所做的 我正在将字符串url从这个片段发送到第一个片段 list.setOnItemClickListener(new OnItemClickListener() { @Override p
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
FragmentC fragment = new FragmentC();
final Bundle bundle = new Bundle();
bundle.putString("position", "http://www.facebook.com"); fragment.setArguments(bundle);}
});
fragment.setArguments(bundle);
当我点击列表项时,我什么也看不到。它不会将我重定向到第一个片段。请帮助我。使用Bundle发送
字符串:
//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
在新片段的onCreateView
中:
//Retrieve the value
String value = getArguments().getString("YourKey");
你必须把你的包裹绑在你的碎片上
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
FragmentC fragment = new FragmentC();
final Bundle bundle = new Bundle();
bundle.putString("position", "http://www.facebook.com"); fragment.setArguments(bundle);}
});
fragment.setArguments(bundle);
然后,改变或替换新的片段
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
FragmentC fragment = new FragmentC();
final Bundle bundle = new Bundle();
bundle.putString("position", "http://www.facebook.com"); fragment.setArguments(bundle);}
});
fragment.setArguments(bundle);
您必须确保字符串位于新片段中。调试 1.如果片段由同一活动托管-您不能强制转换片段的意图。片段作为活动的一部分,它本身不是一个活动。因此,要在片段之间共享字符串,可以在活动中声明静态字符串。从片段A访问该字符串以设置值并在片段B中获取字符串值
2.两个片段都由不同的活动托管-然后您可以使用putExtra将字符串从活动a的片段a传递到活动B。将该字符串存储在活动B中,并在片段B中使用。您可以通过两种方式发送数据
首先,当您要在发送数据时启动该片段时
SecondFragment ldf = new SecondFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
第二,当您想发送数据而不打开片段时
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.add(R.id.fragContainer1, new ModelListFragment(), FRAG_MODEL_LIST);
ft.add(R.id.fragContainer2, new TrimListFragment(), FRAG_TRIM_LIST);
ft.commit();
Fragment fragment = mFragmentManager.findFragmentByTag(MainActivity.FRAG_MODEL_LIST);
Log.d("MY", "found fragment: " + (fragment != null));
你的新片段是什么意思?是你的“另一个片段”。是要接收字符串值的片段。容器引用什么?是在活动xml文件中声明的id。android:id=“@+id/container”确定..它工作..它重定向到第一个片段。但它显示的是选择浏览器查看网站的对话框,而不是在我的webview上显示网站..你能帮我一下吗。我会接受答案。:)我有不同的片段,我想将字符串从片段发送到另一个片段。是的,理解但两个片段都由相同的活动托管??如何接收第二个片段中的数据?