0

iam trying to get the calendar value from other fragment , i have some problems . i cant pass more than 1 value between these fragments and when i call " addfragment" the values for the other edittext replaced with null . Does anyone suggust me to do something different and easier ?

CalendarFragment

 public class CalendarFragment extends Fragment {
android.widget.DatePicker DatePicker;
Button buttonstartSetDialog;
Button buttonCancelAlarm;
TextView textAlarmPrompt;

TimePicker timePicker;
CheckBox optRepeat;

EditText alarmDetailText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);




    DatePicker= (android.widget.DatePicker)rootView.findViewById(R.id.pickerdate);
    optRepeat = (CheckBox)rootView.findViewById(R.id.optrepeat);
    textAlarmPrompt = (TextView)rootView.findViewById(R.id.alarmprompt);
    //alarmDetailText = (EditText)rootView.findViewById(R.id.alarmDetail);
    java.util.Calendar calNow = java.util.Calendar.getInstance();
    java.util.Calendar calSet = (java.util.Calendar) calNow.clone();

    calSet.set(java.util.Calendar.DATE,DatePicker.getYear());
    calSet.set(java.util.Calendar.MONTH,DatePicker.getMonth());
    calSet.set(java.util.Calendar.DAY_OF_MONTH,DatePicker.getDayOfMonth());

    int year= DatePicker.getYear();
    int day = DatePicker.getDayOfMonth();
    int month =DatePicker.getMonth();



    Toast toast = Toast.makeText(getActivity(), " On "  +day+" / "+(DatePicker.getMonth()+1)+" / "+DatePicker.getYear()+ "\n" , Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();


    buttonstartSetDialog = (Button)rootView.findViewById(R.id.startSetDialog);
    buttonstartSetDialog.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            String value = "year";

            AddTaskFragment frag = new AddTaskFragment();
            FragmentManager fm = getFragmentManager();
            Bundle bundle2 = new Bundle();
            bundle2.putString("akey2",value);
            frag.setArguments(bundle2);

            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.frame, frag);
            ft.addToBackStack(null);
            ft.commit();
           // calSet.set(java.util.Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
           // calSet.set(java.util.Calendar.MINUTE, timePicker.getCurrentMinute());
           // calSet.set(java.util.Calendar.SECOND, 0);
           // calSet.set(java.util.Calendar.MILLISECOND, 0);

           /* if(calSet.compareTo(calNow) <= 0){
                //The set Date/Time already passed
                Toast.makeText(getActivity(),
                        "تاريخ أو وقت خاطئ",
                        Toast.LENGTH_LONG).show();


            }*/

           // setAlarm(calSet, optRepeat.isChecked());



        }
    });

    buttonCancelAlarm = (Button) rootView.findViewById(R.id.cancel);
    buttonCancelAlarm.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View arg0) {
        }
    });
    return rootView;


  }
}

The addFragment :

 public class AddTaskFragment extends Fragment {
String quadric ;
String strtext2 ;


public AddTaskFragment(){}
public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);


}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_add_task, container, false);

    Button add ;
    final EditText tasktext;
     final EditText taskdate;
     final EditText tasknote;
    final EditText taskemail , taskdeadline;
    taskdate = (EditText) rootView.findViewById(R.id.editTextDate);
    tasktext = (EditText) rootView.findViewById(R.id.editTexttaskname);
    tasknote = (EditText) rootView.findViewById(R.id.editTextnote);
    taskemail = (EditText) rootView.findViewById(R.id.editTextemail);
    taskdeadline =(EditText) rootView.findViewById(R.id.TaskDeadline);

    final ImageButton calendarIb , calendarIb2;

    Bundle bundle1 = getArguments();
    if (bundle1 != null){
        strtext2 = (String) bundle1.getString("akey2");
        taskdate.setText(strtext2);
        taskdeadline.setText(strtext2);
    }

    Spinner spinner=(Spinner) rootView.findViewById(R.id.spinner1);

    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),R.array.country_array, android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(adapter);

    calendarIb = (ImageButton) rootView.findViewById(R.id.calendarbutton);
    calendarIb2 = (ImageButton) rootView.findViewById(R.id.calendarbutton2);

    calendarIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Fragment frag;
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            frag = new CalendarFragment();
            CalendarFragment llf = new CalendarFragment();
            ft.replace(R.id.fo, frag);
            ft.addToBackStack(null);
            ft.commit();


        }
    });



    calendarIb2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Fragment frag;
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            frag = new CalendarFragment();
            CalendarFragment llf = new CalendarFragment();
            ft.replace(R.id.fo, frag);
            ft.addToBackStack(null);
            ft.commit();



        }
    });




    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

     Toast.makeText(adapterView.getContext(), " "+adapterView.getItemAtPosition(i).toString(), Toast.LENGTH_SHORT).show();
            //quadric = adapterView.getItemAtPosition(i).toString();
            if (i == 0 ){
                quadric = "q1";
            }
            if (i == 1 ){
                quadric = "q2";
            }
            if (i == 2 ){
                quadric = "q3";
            }
            if (i == 3 ){
                quadric = "q4";
            }


                }


        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });




    add = (Button)rootView.findViewById(R.id.addbutton);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ParseObject showlist = new ParseObject("AllTasks");
            ParseUser getUserInfo = new ParseUser().getCurrentUser();
            String username = getUserInfo.getUsername();




            if(tasktext.getText().toString().trim().length() == 0){
                //empty
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("Task name should not be empty!");
                builder.setTitle("Error ");
                builder.setPositiveButton("ok", null);
                builder.setNegativeButton("cancel", null);
                builder.create();
                builder.show();
            }
            else {
                showlist.put("TaskN", tasktext.getText().toString());
                taskdate.setText(strtext2);

                showlist.put("Date", taskdate.getText().toString());
                showlist.put("Note", tasknote.getText().toString());
                showlist.put("Email", taskemail.getText().toString());
                showlist.put("Quadric",quadric);
                showlist.put("TheUser", username);

                Toast.makeText(getActivity(), "Saved", Toast.LENGTH_LONG).show();
                if(taskemail.getText().toString().trim().length()!= 0 && tasktext.getText().toString().trim().length()!= 0){
                    //empty
                    Intent i = new Intent(Intent.ACTION_SEND);

                    i.setType("message/rfc822");
                    i.putExtra(Intent.EXTRA_EMAIL, new String[]{taskemail.getText().toString()});
                    i.putExtra(Intent.EXTRA_SUBJECT, tasktext.getText().toString());
                    i.putExtra(Intent.EXTRA_TEXT, " ");

                    getActivity().startActivity(Intent.createChooser(i, "Send mail..."));
                    Fragment frag;
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    frag = new AllTaskFragment();
                    AllTaskFragment llf = new AllTaskFragment();
                    ft.replace(R.id.fo, frag);
                    ft.addToBackStack(null);

                    ft.commit();
                }
                else {

                    Fragment frag;
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    frag = new AllTaskFragment();
                    AllTaskFragment llf = new AllTaskFragment();
                    ft.replace(R.id.fo, frag);
                    ft.addToBackStack(null);

                    ft.commit();
                }

            }



            showlist.saveInBackground(new SaveCallback() {
                                          @Override
                                          public void done(ParseException e) {
                                              taskdate.setText("");
                                              tasknote.setText("");
                                              tasktext.setText("");


                                          }
                                      }
                );


        }
        }
    );
    return rootView;


  }

}                                    

Here are the screen shot :

so here before calling the calendar

and here after calling the calendar , the task name ie empty now

user3604741
  • 33
  • 1
  • 8

2 Answers2

0

Your are getting the 'year' value as an int like this

int year = DatePicker.getYear();

So when you create your bundle you need to pass this year value like this -

bundle2.putInt("akey2", year);

Where as you are creating a new String String value = "year"; and using that. That is why you are getting null values in other fragment. You want the actual int value of the year not the String "year" itself.

Also you can put as many values as you want like -

bundle2.putInt("akey2", year);
bundle2.putInt("akey3", day);
Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
  • Yes, I know I am retrieving a string value which is wrong but the null value is for the other text fields, Like if I set the task name then go to the calendar fragment to get the year if I return to addFragment the task name will be empty. – user3604741 Mar 02 '16 at 08:35
  • You need to maintain the state of the fragment. Check [this](http://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack) to understand. – Jyotman Singh Mar 02 '16 at 08:38
  • Thank u i will consider that , but i have a question how can i retrieve multiple bundles ? – user3604741 Mar 02 '16 at 11:45
  • Bundle is only 1 but you can add as many values to bundle as you want. Like I've mentioned in the answer. – Jyotman Singh Mar 02 '16 at 12:06
0

You can try this follwing

String value=getArguments().getString("akey2")
Rathod Vijay
  • 417
  • 2
  • 7