我在我的Activity Adapter中显示一个Dialog Fragment,我在Dialog片段中有一个EditText和一些其他视图。我必须将一些数据直接返回到Activity和Adapter,单击Dialog Dragment中的更新按钮我可以将数据传递给Activity,但是无法将数据传递给adapter.My适配器监听器无法正常工作
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");
}
}
}
}
我的适配器类
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements View.OnClickListener,MyDialogFragment.OnSetChangeListenerDialog{
@Override
public void returnData(String result) {
setValue = result;
}
}
答案 0 :(得分:0)
尝试定义外部简单接口:
Returning.java
public interface Returning {
void return_value(String value);
}
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
public class MyAdapter implements Returning {
// ...
@Override
public void return_value(String value) {
setValue = value;
}
}
答案 1 :(得分:0)
将此方法添加到对话框类
public interface EditDialogListener {
void receiveResult(String inputText);
}
当您想要将值返回到主活动时,只需要使用此方法
EditDialogListener activity = (EditDialogListener) getActivity();
activity.receiveResult(YourEditText.getText().toString());
现在你的活动
public class YourActivity extends Activity implements EditDialogListener
添加receiveResult
方法
public void receiveResult(String str) {
textReturned.setText(str);//set the value on an `editText` at your activity
}