[英]How to return data from a dialog fragment to activity adapter?
I am showing a Dialog Fragment from my Activity Adapter, I have one EditText and some other views in Dialog fragment. 我正在从活动适配器显示一个对话框片段,在对话框片段中有一个EditText和其他一些视图。 And I have to return some data directly to Activity and the Adapter clicking the update button in Dialog Dragment I can pass data to the Activity, but can't pass data to adapter.My adapter listener is not working
我必须直接将一些数据返回给Activity和适配器,然后单击Dialog Dragment中的更新按钮,我可以将数据传递给Activity,但是不能将数据传递给Adaptor。我的适配器侦听器不起作用
public class MyDialogFragment extends android.support.v4.app.DialogFragment
{
private OnItemUpdateListenerDialog onItemUpdateListenerDialog;
private void SetValues(final CartItemDetail item) {
buttUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setValue=editSet.getText().toString();//get value from editText
onItemUpdateListenerDialog.onItemUpdateRequest(addItemToCart(item.getDataOne(), item.getDataTwo()));//listner for activity
onSetChangeListnerDialog.returnData(setValue);//listner for adapter
dismiss();
}
});
}
public interface OnItemUpdateListenerDialog {
void onItemUpdateRequest(JSONObject jsonObject);
}
public void setOnItemUpdateListenerDialog(OnItemUpdateListenerDialog onItemUpdateListener) {
this.onItemUpdateListenerDialog = onItemUpdateListenerDialog;
}
public interface OnSetChangeListenerDialog {
void returnData(String result);
}
public void setOnSetChangeListenerDialog(OnSetChangeListenerDialog onSetUpdateRequest) {
onSetChangeListnerDialog = onSetChangeListnerDialog;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
this.onItemUpdateListenerDialog = (OnItemUpdateListenerDialog)activity;//activit listner
this.onSetChangeListnerDialog = (OnSetChangeListenerDialog)activity;//adapter listner
}
catch (final ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
}
}
}
}
My Adapter class
我的适配器类
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements View.OnClickListener,MyDialogFragment.OnSetChangeListenerDialog{
@Override
public void returnData(String result) {
setValue = result;
}
}
Try to define an external simple interface : 尝试定义一个外部简单接口:
Returning.java Returning.java
public interface Returning {
void return_value(String value);
}
MyDialogFragment.java MyDialogFragment.java
public class MyDialogFragment {
private Returning returning;
buttUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
returning= ((Returning ) context);
returning.return_value("everything");
// ...
}
}
}
MyAdapter.java MyAdapter.java
public class MyAdapter implements Returning {
// ...
@Override
public void return_value(String value) {
setValue = value;
}
}
Add this method to your dialog class 将此方法添加到对话框类
public interface EditDialogListener { void receiveResult(String inputText); }
when you want to return the value to the main activity just impimint this method 当您想将值返回到主活动时,只需使用此方法
EditDialogListener activity = (EditDialogListener) getActivity(); activity.receiveResult(YourEditText.getText().toString());
now on your activity 现在进行您的活动
public class YourActivity extends Activity implements EditDialogListener
add the receiveResult
method 添加
receiveResult
方法
public void receiveResult(String str) { textReturned.setText(str);//set the value on an `editText` at your activity }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.