user2154328
user2154328

Reputation: 53

Call method from another class in OnClick

I have a class that contains:

 @Override
public void onClick(View createView) {
    switch (createView.getId()) {
    case R.id.save_button:
        Appointments add = new Appointments();
        add.addAppointment(TITLE);
        add.addAppointment(TIME);           
        break;
    }
}

I want to be able to call the addAppointment method which is in another class:

 public void addAppointment(String string) {
  // Insert a new record into the Appointment data source.
  // You would do something similar for delete and update.
  SQLiteDatabase db = events.getWritableDatabase();
  ContentValues values = new ContentValues();
  values.put(TITLE, string);
  values.put(TIME, string);
  db.insertOrThrow(TABLE_NAME, null, values);
 }

I have tried this way, but when I click the onclick , the program crashes

Upvotes: 0

Views: 1543

Answers (2)

codeMagic
codeMagic

Reputation: 44571

 @Override
public void onClick(View createView) {
    switch (createView.getId()) {
    case R.id.save_button:
        Appointments add = new Appointments();
        add.addAppointment(TITLE, TIME);  //Send both together
        break;
    }
}
I want to be able to call the addAppointment method which is in another class:

 public void addAppointment(String title, String time) {  // Change method header to accept both Strings here
  // Insert a new record into the Appointment data source.
  // You would do something similar for delete and update.
  SQLiteDatabase db = events.getWritableDatabase();
  ContentValues values = new ContentValues();
  values.put("title", title);   // second param is what you passed in for first param of method
  values.put("time", time);    // second param here is whatever you passed in for second param of method
  db.insertOrThrow(TABLE_NAME, null, values);
 }

I don't know what TITLE and TIME are because I don't see them declared or initialized anywhere but inside the vaules.put() should be (key, value) so you make the key something descriptive about the value and the value obviously just that. Normally all caps represent a constant so just something to think about to keep up with standards

Upvotes: 2

yuva ツ
yuva ツ

Reputation: 3703

write method with two parameters 1st.calling method one after other with one parameter won't give you both values.

 add.addAppointment(TITLE,TIME);

Upvotes: 1

Related Questions