vendredi 30 mars 2018

Fragment is not grabbing my IntentData

I have a first fragment which I send data to another fragment depending on which button they press by

mButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), AddItemsActivity1.class);
            intent.putExtra("ToButton", 1);
            ((AddItemsActivity1)getActivity()).ToNextPage(view);
        }
    });

mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AddItemsActivity1.class);
                intent.putExtra("ToButton", 2);
                ((AddItemsActivity1)getActivity()).ToNextPage(view);
            }
        });

The buttons are sending data to it's own fragment's activity.

Then I retrieve my Data in my other Fragment that is also in the AddItemsActivity1 activity by

int toButton = getActivity().getIntent().getIntExtra("ToButton", 0);

then depending on the value it gets it adds to a certain child in my database.

if (toButton == 1) {
                    String key = mDairyDataBase.push().getKey();
                    HashMap<String, String> dataMap = new HashMap<>();
                    dataMap.put("Name", item);
                    dataMap.put("Key", key);
                    mDairyDataBase.child(key).setValue(dataMap);
}

 else if (toButton == 2){
                String key = mFruitsDataBase.push().getKey();
                HashMap<String, String> datamap = new HashMap<>();
                datamap.put("Name", item);
                datamap.put("Key", key);
                mFruitsDataBase.child(key).setValue(datamap);
}

My problem is that it doesn't return a value so it never adds to the database. When I change the default value of toButton to 1 then it adds it to the mDairyDatabase and same goes for the other one.

Aucun commentaire:

Enregistrer un commentaire