0

I have reached the wall on the following. I am trying to pass a variable from my main activity to a java class that generates my database because I want to use this variable in one of my queries of that database to get a result and then pass it to the Main activity. Here's my piece of code:

//MainActivity

public class MainActivity extends AppCompatActivity {

    private static RadioGroup selectedAvgStds;
     ...... //rest of the code/// 


    public void onClickListenerButton(){


    selectedAvgStds = (RadioGroup)findViewById(R.id.controlAverageOfLiving);

    showResults.setOnClickListener(


        int avgStdLiving        = selectedAvgStds.getCheckedRadioButtonId();
        selectedAvgStdsRb       = (RadioButton) findViewById(avgStdLiving);

        //variable that  I want to pass 
        String avgStdLivingText = (String) selectedAvgStdsRb.getText();

        switch (option) {
           case "one":
               Intent intent = new Intent(MainActivity.this,DatabaseHelper.class);
               Intent.putExtra("values",avgStdLivingText);
               startActivity(intent);
           break;

          }
    );
}

Piece of code of my database

//DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper{

public Cursor showResults(){

    SQLiteDatabase db = this.getWritableDatabase();

    //the intent does NOT work
    Bundle bundle = getIntent().getExtras();

    Cursor results = db.rawQuery("select * from "+TEMP_TABLE+"where value = " + selectedAvgStds , null);
    return results;
     }
}

The intent is not working despite the fact I have imported all the Intent libraries in the activity and the class. How can I achieve my goal? Why the Intents do not work here?

Any suggestion and idea will be enormously appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
abautista
  • 2,410
  • 5
  • 41
  • 72
  • Your DatabaseHelper class isn't an Activity, so calling startActivity() on it clearly wont work :/ Where do you initialise your Database helper? – Ed Holloway-George Sep 03 '16 at 22:36
  • What about parametrizing your `showResults()` method? – Phantômaxx Sep 03 '16 at 22:43
  • Your logic is flawed, clearly using `startActivity(intent);` does not work because `DatabaseHelper extends SQLiteOpenHelper` not an `Activity`. However I wouldn't go about changing that. Why not just have an instance of `DatabaseHelper` in your Activity and amend your method take a variable parameter? – Mark Sep 03 '16 at 22:44
  • 1
    i dont know why people are down voting this question? I thought this is a learning forum. – hariszhr Sep 03 '16 at 22:49
  • 1
    from this question i learnt that we cannot use default interface methods in Android at the moment. I was trying to find a solution with that. This is not a bad question. – hariszhr Sep 03 '16 at 22:51
  • Thanks @hariszhr for your comment. I am not that android savvy and I was concerned that the startActivity(intent) was not going to work as described in http://stackoverflow.com/questions/30559053/android-getintent-is-deprecated . How can I achieve this parametrizing method() ? – abautista Sep 03 '16 at 22:55
  • @EdGeorge I initalize my database helper in my main activity on the method protected void onCreate(). – abautista Sep 03 '16 at 23:01
  • I think it means that u should try using databasehelper as a normal class. Create an object of that class inside onclicklistenerbutton method. And change the showresult method to take avgstdlivingtext as a parameter. Then do all the processing inside showresult method, as u r doing already. Try it. – hariszhr Sep 03 '16 at 23:06

2 Answers2

1

As per your comment, why do you not simply make DatabaseHelper an instance variable and parameterize your showResults method as following:

public class MyActivity extends Activity {

    private DatabaseHelper myDatabaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //initialise your helper here
        myDatabaseHelper = ...
    }

    public void onClickListenerButton(){

        // All your other stuff here...

        // variable that  I want to pass
        String avgStdLivingText = selectedAvgStdsRb.getText().toString();
        myDatabaseHelper.showResults(avgStdLivingText);
    }

}

And then within the helper class you can simply do:

public Cursor showResults(String selectedAvgStds){
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor results = db.rawQuery("select * from "+TEMP_TABLE+"where value = " + selectedAvgStds , null);
        return results;
     }
}
Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
0

The reason you are not able to get data using intent is that SQLiteOpenHelper class might not have the method definition for getIntent(). I would prefer you use Shared Preferences to store the data and retrieve it inside you SQLiteOpenHelper class.

mrtpk
  • 1,398
  • 4
  • 18
  • 38
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45