[英]String doesn't update when passed to different Fragment?
I'm trying to send two strings from Fragment A to Fragment B. Currently, i've implemented an interface
listener like this... 我正在尝试将两个字符串从片段A发送到片段B。当前,我已经实现了这样的interface
侦听器...
Fragment A Method: 片段方法:
String arrayTitle1 = arrayList.get(Index).get("ArrayTitle1");
String arrayTitle2 = arrayList.get(Index).get("ArrayTitle2");
((TextFooterListener) getActivity()).onTextFooterListener(arrayTitle1, arrayTitle2);
I pass these strings to the Activity which sends them to Fragment B like this... 我将这些字符串传递给Activity,然后将其发送到Fragment B,如下所示:
Activity: 活动:
@Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
FragmentB.arrayTitle1 = arrayTitle1;
FragmentB.arrayTitle2 = arrayTitle2;
}
Fragment B: 片段B:
They are then received and stored in Fragment B as public strings. 然后将它们作为公共字符串接收并存储在片段B中。
public String arrayTitle1;
public String arrayTitle2;
And in the onCreateView
of Fragment B, i try to assign these strings. 在片段B的onCreateView
中,我尝试分配这些字符串。
arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);
But unfortunately, whenever Fragment A's method gets called, the onTextFooterListener
doesn't seem to update the strings. 但是不幸的是,每当调用Fragment A的方法时, onTextFooterListener
似乎都不会更新字符串。
Any suggestions? 有什么建议么?
You change the values in Fragment B but never actually reassign the new values into the TextViews. 您更改了片段B中的值,但从未真正将新值重新分配到TextView中。
Change 更改
@Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
FragmentB.setTitles(arrayTitle1, arrayTitle2);
}
And in Fragment B : 在片段B中:
public void setTitles(String title1, String title2) {
arrayTitle1 = title1;
arrayTitle2 = title2;
arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);
}
There is a mechanism you need to use to pass data to a Fragment. 您需要使用一种机制将数据传递给Fragment。 Please consider the following approach: 请考虑以下方法:
@Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
Bundle data = new Bundle();
data.putString("array1", arrayTitle1);
data.putString("array2", arrayTitle2);
FragmentB b = FragmentB();
b.setArguments(data);
}
Then in your onCreateView
of FragmentB: 然后在您的FragmentB的onCreateView
中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
arrayTitle1 = getArguments().getString("array1");
arrayTitle2 = getArguments().getString("array2");
arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);
return inflater.inflate(R.layout.fragment, container, false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.