简体   繁体   中英

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. 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

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

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;
    }
}

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

public void receiveResult(String str) { textReturned.setText(str);//set the value on an `editText` at your activity }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM