如何将数据从活动传递到Java类 [英] How to pass data from an activity to a java class

查看:72
本文介绍了如何将数据从活动传递到Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经到达下面的墙.我试图将变量从我的主要活动传递给生成我的数据库的Java类,因为我想在该数据库的查询中使用此变量来获取结果,然后并将其传递给主要活动.这是我的代码:

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;

          }
    );
}

我数据库的代码段

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

尽管我已经在活动和类中导入了所有Intent库,但该Intent无法正常工作.我如何实现我的目标?为什么意图在这里不起作用?

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.

推荐答案

根据您的评论,为什么不简单地将 DatabaseHelper 设置为实例变量并为您的 showResults 参数化方法如下:

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

}

然后在helper类中,您可以简单地执行以下操作:

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

这篇关于如何将数据从活动传递到Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆