vendredi 24 juin 2016

Android: Data between Fragments and Activities

I'm trying to pass data between a fragment and an activity and I can't. I get no errors or exceptions. On my fragment I have the vallue and on activity that value is null. I'm using interfaces.

Code of HoroscopeChoice Fragment, which is the fragment with buttons. Each button has a value, which I want to pass to the activity every time I push them.

(...)
  static OnInfoChangedListener mCallback;

 public HoroscopeChoice() {}
    /******************************
     * Callback
     ********/
    public static void OnInfoChangedListener(OnInfoChangedListener callback) {
      mCallback = callback;
    }
    public interface OnInfoChangedListener {
        public void onInfoChanged(String horosocopo);
     }
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_horoscope_choice,
                container, false);

        Button aquarius;
        aquarius  = (Button) view.findViewById(R.id.aquarius1);

        final int id = view.getId();

        View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String horoscopo = onClick2(v.getId());
                Log.d("HoroscopeChoice", "push button->"+horoscopo);
                mCallback.onInfoChanged(horoscopo);
            }
        };

        aquarius.setOnClickListener(onClickListener);

public String onClick2(int id)
{
    String horoscopo="";

    if (id == R.id.aquarius1) {
        horoscopo = "Aquarius";
    }
 }
(...)

Code of the Activity:

(...)
public void onInfoChanged(String horoscopo) {
        Log.d("SchedulerActivity","OnInfoChanged na Scheduler->"+horoscope);

        mHoroscopeDisplay = (TextView) findViewById(R.id.dailyHoroscope4);
        mHoroscopeDisplay.setText(horoscopo);
    }

When I do Log.d in the Fragment I get a value, on the Activity I have no value. Does anyone knows what is wrong?

Aucun commentaire:

Enregistrer un commentaire