从OnClick中的另一个类调用方法

时间:2013-03-24 15:15:26

标签: android

我有一个包含以下内容的课程:

 @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;
    }
}

我希望能够调用另一个类中的addAppointment方法:

 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);
 }

我试过这种方式,但是当我点击onclick时,程序崩溃了

2 个答案:

答案 0 :(得分:2)

 @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);
 }

我不知道TITLE和TIME是什么,因为我没有看到它们在任何地方声明或初始化,但在vaules.put()内应该是(key, value)所以你要key描述一些东西关于valuevalue显然就是这样。通常情况下,所有上限代表constant,因此只需要考虑跟上标准

答案 1 :(得分:1)

使用两个参数写入方法1st.calling方法一个接一个地使用一个参数不会给你两个值。

 add.addAppointment(TITLE,TIME);