将 Spinner 选定的项目从 Fragment 传递到 Activity
Pass Spinner Selected Items from Fragment to Activity
不完全理解片段生命周期。
案例:
Click FrameLayout
from Activity
to move to Fragment
is working.
问题:
In Fragment
there are two spinners
and one SubmitButton
, when selected both spinner
values, the clicking ofSubmitButton
, should display those values from spinner back to the Activity's
two Textviews
. But, I am unable to do that.
我的解决方案:
I tried to use Intent
, putExtras
and then getExtras
, but as we are in Fragment
, Intent
is not going to work for Fragment
. Bundle
also not helping.
P.S. 需要了解好的 Fragment 生命周期的人。阅读来自 Whosebug 和其他教程的许多帖子。没有找到我的意思。
不需要像 eventBus 这样的外部库
有两种方法可以做到这一点
1) 将 getActivity() 转换为您的 Activity 并调用特定方法并传递参数。
public class MyActivity extends AppCompatActivity {
public void setData(String value){
// do whatever with the data
}
}
public class MyFragment extends Fragment{
public void someMethod(){
((MyActivity)getActivity).setData(your_data);
}
}
2) 创建一个接口并将值传递给activity.
public class MyActivity extends AppCompatActivity implements SpinnerListener{
@Override
public void onSpinnerItemSelected(String value){
// do whatever with the data
}
public interface SpinnerListener {
void onSpinnerItemSelected(String value);
}
}
public class MyFragment extends Fragment {
private SpinnerListener spinnerListener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if(context instanceOf MyActivity)
spinnerListener = ((MyActivity)context);
}
public void someMethod() {
if(spinnerListener != null)
spinnerListener.onSpinnerItemSelected(your_data);
}
}
注意:安全的方法是使用接口。
不完全理解片段生命周期。
案例:
Click
FrameLayout
fromActivity
to move toFragment
is working.
问题:
In
Fragment
there are twospinners
and oneSubmitButton
, when selected bothspinner
values, the clicking ofSubmitButton
, should display those values from spinner back to theActivity's
twoTextviews
. But, I am unable to do that.
我的解决方案:
I tried to use
Intent
,putExtras
and thengetExtras
, but as we are inFragment
,Intent
is not going to work forFragment
.Bundle
also not helping.
P.S. 需要了解好的 Fragment 生命周期的人。阅读来自 Whosebug 和其他教程的许多帖子。没有找到我的意思。
不需要像 eventBus 这样的外部库
有两种方法可以做到这一点
1) 将 getActivity() 转换为您的 Activity 并调用特定方法并传递参数。
public class MyActivity extends AppCompatActivity {
public void setData(String value){
// do whatever with the data
}
}
public class MyFragment extends Fragment{
public void someMethod(){
((MyActivity)getActivity).setData(your_data);
}
}
2) 创建一个接口并将值传递给activity.
public class MyActivity extends AppCompatActivity implements SpinnerListener{
@Override
public void onSpinnerItemSelected(String value){
// do whatever with the data
}
public interface SpinnerListener {
void onSpinnerItemSelected(String value);
}
}
public class MyFragment extends Fragment {
private SpinnerListener spinnerListener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if(context instanceOf MyActivity)
spinnerListener = ((MyActivity)context);
}
public void someMethod() {
if(spinnerListener != null)
spinnerListener.onSpinnerItemSelected(your_data);
}
}
注意:安全的方法是使用接口。