2

I have the following code which creates an event when a button is clicked. I would like to know how to create more than one, (Say 5) events by pressing this button. Thank you for any help!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Button button = (Button)findViewById(R.id.button1);
   // button.setOnClickListener(this);
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
    final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);


    final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) {


             if (checkBox.isChecked()) {

                 { 

                SimpleDateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mmaa"); 
                long lnsTime = 0, lneTime = 0;

                        Date dateObject;

                        try{
                        String dob_var = "August 16, 2012, 11:35PM";

                        dateObject = formatter.parse(dob_var);

                        lnsTime = dateObject.getTime();
                        Log.e(null, Long.toString(lnsTime));

                        dob_var = "August 16, 2012, 11:59PM";      

                        dateObject = formatter.parse(dob_var);

                        lneTime = dateObject.getTime();
                        Log.e(null, Long.toString(lneTime));
                        }

                        catch (java.text.ParseException e) 
                            {
                            // TODO Auto-generated catch block
                                e.printStackTrace();
                                Log.i("E11111111111", e.toString());
                            }

                     Intent intent = new Intent(Intent.ACTION_EDIT);

                         intent.setType("vnd.android.cursor.item/event");
                      intent.putExtra("title", "9:00AM Start");
                      intent.putExtra("description", "There will be a 9:00AM start tomorrow.");
                      intent.putExtra("eventLocation", "Chris' house");
                      intent.putExtra("beginTime", lnsTime);
                      intent.putExtra("endTime", lneTime);
                      startActivity(intent);

                 }



        } 

The code creates the single event just fine so far, its just I need multiple to be created, preferably without the "save/cancel" screen.

ScorpioBlue
  • 189
  • 1
  • 5
  • 17

1 Answers1

0

You could do some iterations over a for-loop when you click that button.

Like

for (int i = 0; i < YOUR_NUMBER_OF_EVENTS; i++)
    createCalendarEvent();

Where the method

createCalendarEvent();

does the part of creating events. This could be done directly when clicking the button, or done in a Async task to prevent the GUI from freezing and/or blocking.

I have not tested your code or the one in the tutorial, but you could read this tutorial http://www.developer.com/ws/android/programming/Working-with-the-Android-Calendar-3850276.htm to learn how to work with calendar events without starting a new intent. (From the post https://stackoverflow.com/a/3722180/1513735)

Community
  • 1
  • 1
Heskja
  • 787
  • 6
  • 19