0

我有一个类,其中包含:

 @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 时,程序崩溃了

4

2 回答 2

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)所以你可以对 the 进行key描述valuevalue显然就是这样。通常所有的大写字母都代表一个constant需要考虑的东西以跟上标准

于 2013-03-24T15:24:19.380 回答
1

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

 add.addAppointment(TITLE,TIME);
于 2013-03-24T15:20:32.320 回答