如何从对话片段接收数据到 android 中的 activity
how to receive the data from dialog fragment to activity in android
我正在调用 DialogFragment 从 Activity 到 select 状态(DialogFragment 中的 RecyclerView)。
但我不确定如何接收 selected 状态。
我知道 Recyclerview 的使用,主要任务是将数据从 DialogFragment.
发送回之前的 activity
从 Activity 到 Activity 我可以使用 startActivityForResult(intent, code)
,但是从 Activity to DialogFragment
我没有得到这个。
Activity1 ---> DialogFragment ---> Activity1
谁能帮帮我。提前致谢。
首选方法是使用回调从片段中获取信号。另外,这是Android在Communicating with the Activity
提出的推荐方法
对于您的示例,在您的 DialogFragment
中添加一个接口并注册它。
public static interface OnCompleteListener {
public abstract void onComplete(String time);
}
private OnCompleteListener mListener;
// make sure the Activity implemented it
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
this.mListener = (OnCompleteListener)activity;
}
catch (final ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
}
}
现在在您的Activity
中实现这个接口
public class MyActivity extends Activity implements MyDialogFragment.OnCompleteListener {
//...
public void onComplete(String time) {
// After the dialog fragment completes, it calls this callback.
// use the string here
}
}
现在在您的 DialogFragment
中,当用户单击“确定”按钮时,通过您的回调将该值发送回 Activity
。
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String time = Integer.toString(hourOfDay) + " : " + Integer.toString(minute);
this.mListener.onComplete(time);
}
您可以以类似的方式使用 cal back 传递选定的列表值
您可以在 activity
中使用接口调用它
public 接口 OnChangeListener {
void onChange(int state);
}
我正在调用 DialogFragment 从 Activity 到 select 状态(DialogFragment 中的 RecyclerView)。
但我不确定如何接收 selected 状态。
我知道 Recyclerview 的使用,主要任务是将数据从 DialogFragment.
发送回之前的 activity
从 Activity 到 Activity 我可以使用 startActivityForResult(intent, code)
,但是从 Activity to DialogFragment
我没有得到这个。
Activity1 ---> DialogFragment ---> Activity1
谁能帮帮我。提前致谢。
首选方法是使用回调从片段中获取信号。另外,这是Android在Communicating with the Activity
提出的推荐方法对于您的示例,在您的 DialogFragment
中添加一个接口并注册它。
public static interface OnCompleteListener {
public abstract void onComplete(String time);
}
private OnCompleteListener mListener;
// make sure the Activity implemented it
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
this.mListener = (OnCompleteListener)activity;
}
catch (final ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
}
}
现在在您的Activity
public class MyActivity extends Activity implements MyDialogFragment.OnCompleteListener {
//...
public void onComplete(String time) {
// After the dialog fragment completes, it calls this callback.
// use the string here
}
}
现在在您的 DialogFragment
中,当用户单击“确定”按钮时,通过您的回调将该值发送回 Activity
。
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String time = Integer.toString(hourOfDay) + " : " + Integer.toString(minute);
this.mListener.onComplete(time);
}
您可以以类似的方式使用 cal back 传递选定的列表值
您可以在 activity
中使用接口调用它public 接口 OnChangeListener { void onChange(int state);
}