简体   繁体   English

如何将数据从对话框片段返回到活动适配器?

[英]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.

相关问题 如何将对话框片段值从对话框返回到主要活动? - How to return dialog fragment values from dialog to main activity? 将数据从活动发送到对话框,其中自定义适配器和我的自定义适配器在片段中 - Send Data from Activity to Dialog which in Custom adapter and my custom adapter is in fragment 如何将数据从适配器返回到活动 - How to return data from adapter to activity 如何从 recyclerview 适配器关闭对话框片段 - How to dismiss a dialog fragment from a recyclerview adapter 如何将数据从返回活动传递到先前活动中的对话框? - How to pass data from return activity to dialog in previous activity? 如何通过活动通知片段中的适配器? - How to notify an Adapter in a Fragment from Activity? 将数据从对话框片段返回到调用它的活动 - Returning Data from a Dialog Fragment to the Activity that Called It 如何从对话框片段中打开新活动 - How to open a new activity from a dialog fragment 如何将数据从活动发送到扩展对话框片段的自定义对话框 - How to send data from an Activity to a custom dialog which extends Dialog Fragment 在同一活动中使用startActivityForResult将数据从适配器传递到片段 - Passing data from a adapter to fragment using startActivityForResult within same activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM