如何将数据从Activity发送到Fragment?(Android) [英] How to send data from Activity to Fragment?(Android)
问题描述
我想利用片段中的一些TextViews(cityField,updatedField).我知道在活动中使用它们会更容易,但是问题是由于要获取一些JSON数据,我不得不在Fragment上加入一些代码
I want to make use of some TextViews(cityField, updatedField) I have in my activity inside my fragment. I know it would have been easier to make use of them in the activity instead, but the problem is that I have to join in with some codes on Fragment due to getting some JSON data
我已经获得了活动代码的ID
Already I've gotten the id for the codes on activity
cityField = findViewById(R.id.textView4);
updatedField = findViewById(R.id.textView9);
现在我想在片段中使用它们所以问题是-可能吗?如果可能的话,怎么办?
Now I want to make use of them in my fragment So the question is - is it possible? if it's possible, how?
我已经在此网站上检查了一些答案-从活动在Android中进行分段如何使用捆绑包将数据从活动"传递到片段
Already, I checked some answers on this site - Send data from activity to fragment in Android How to pass data from Activity to Fragment using bundle
但是他们直接不能解决我的问题.
but they directly didn't solve my problem.
推荐答案
您可以像这样从片段中访问这些实例:
You can access these instances from your fragment like this:
字符串cityFieldFragment =(活动为YourActivity).cityField;
字符串updatedFieldFragment =(活动为YourActivity).updatedField;
如果我理解这一权利,则该片段存在于您的活动中,因此您可以从中访问数据,只需确保实例是公共的即可.
If I understood this right, the fragment lives in your activity, so you are able to access the data from it, just make sure that the instances are public.
这篇关于如何将数据从Activity发送到Fragment?(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!