0

I wrote code for SQLite project, and wrote function, that had to use this code. Now I want to attach this function to a button, that appears in another activity. How can I do that? I tried to make onClickListener, or write function in onCreate of main acitivity, but I didn't quite got result.

This is my function:

  private void insertInDB(Seifim seifim){
    SQLiteDatabase db = helper.getReadableDatabase();
    String sql = "INSERT INTO seifim (misparRehev, shemEhida, sugTipul, kamutKilometrim, date, manoa, delek, higui, blamim, zmigim, hashmal, hashlada, abs, mivhan) values('" +
            seifim.getMisparRehev() + "','" + seifim.getShemEhida() + "','" + seifim.getSugTipul() + "','" + seifim.getKamutKilometrim() + "','" +
            seifim.getDate() + "','" + seifim.getManoa() + "','" + seifim.getDelek() + "','" + seifim.getHigui() + "','" +
            seifim.getBlamim() + "','" + seifim.getZmigim() + "','" + seifim.getHashmal() + "','" + seifim.getHashlada() + "','" +
            seifim.getAbs() + "','" + seifim.getMivhan();
    db.execSQL(sql);
    Toast.makeText(getApplicationContext(), "tofes was saved", Toast.LENGTH_LONG).show();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

When you call this method from other activities you should make it public and then put the name of your activity and then the name of the method

Like this,

SecondActivity.insertInDB(args);

You constructed the method this way

 private void insertInDB(Seifim seifim) {}

This means you should pass an instance of Seifim from MainActivity to the method call

Seifim seifim = new Seifim();
//set all the param to the object and then pass it to the method
SecondActivity.insertInDB(seifim);

If you don't want to create a Seifim in MainActivity you should either rewrite your method so you won't pass the args and call it from MainActivity there or make your instance of Seifim from SecondActivity static.

Rainmaker
  • 10,294
  • 9
  • 54
  • 89