1

How can I pass values from a OnItemSelectedListener class (attached to a spinner) to a DatabaseHelper (SQLiteOpenHelper) class?

I'm trying to use setters on DatabaseHelper but for some reason it's just not working. :|

MyOnItemSelectedListener:

public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {

    switch (parent.getId()) {
    case R.id.spinner1:
        DatabaseHelper myDbHelper = new DatabaseHelper(getBaseContext());
        myDbHelper = new DatabaseHelper(this);
        stringc = String.valueOf(parent.getSelectedItemId());
        Toast.makeText(parent.getContext(), "Carreira: " + stringc,
                Toast.LENGTH_LONG).show();
        myDbHelper.setC(stringc);
        break;
    case R.id.spinner2:
        stringpf = String.valueOf(parent.getSelectedItemId());
        Toast.makeText(parent.getContext(), "PFuncionamento: " + stringpf,
                Toast.LENGTH_LONG).show();
        DatabaseHelper.setPF(stringpf);
        break;
    }

DatabaseHelper:

public void setC(String stringC) {
c = stringC;
}

...

public static Cursor getPFunc() {
    return bd
            .rawQuery(
                    "SELECT * FROM PeriodoFuncionamento a INNER JOIN Horario b ON a._id=b.PeriodoFuncionamentoID WHERE b._id = ? GROUP BY a._id",
                    new String[] { c });

Thanks in advance. :)

  • `it's just not working` best problem description ever! – WarrenFaith Jun 17 '11 at 11:44
  • @WarrenFaith, I appreciate your help... if you're willing to read the thread properly. It's just not that difficult, is it? – Kookie_Monster Jun 17 '11 at 11:47
  • @Kookie_Monster: please provide the important parts of your DatabaseHelper, where you use the `C` variable. Also be advised that a variable is recommended to start with a lower case. – WarrenFaith Jun 17 '11 at 11:58
  • Why you creating myDbHelper `DatabaseHelper myDbHelper = new DatabaseHelper(getBaseContext()); myDbHelper = new DatabaseHelper(this);` twice? I think you need to remove second line. – jamapag Jun 17 '11 at 12:01
  • @jamapag: Probably... but it still didn't solve the problem. The first spinner is supposed to help fill a second one, based on the selection. But the activity times out 'cause the query never gets the value. – Kookie_Monster Jun 17 '11 at 13:03
  • @Kookie_Monster: looks like you call `setC()` and `getPFunc()` methods for different instances of `DatabaseHelper` class – jamapag Jun 17 '11 at 14:06

0 Answers0