在方法中使用正确的上下文来获取 sqlite 数据库

发布于 2024-10-07 22:36:43 字数 2382 浏览 2 评论 0原文

我基本上试图从另一个类中的 sqlite 数据库获取信息。我在 onCreate 方法中完成此操作没有任何问题,但是当我尝试在 onCreate 内部的 onClickListener 中使用它时,我找不到要使用的正确上下文。我的代码如下:

private AutoCompleteTextView search;
private Button showInfoButton;
private TextView courseInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_courses);
    final Context baseContext = getBaseContext();
    courseInfo = (TextView) this.findViewById(R.id.text);
    DataBase db = new DataBase(this.getApplicationContext());
    db.openDataBase();
    final ArrayList<String> aCourses = db.getCoursesArr();
    db.close();

    search = (AutoCompleteTextView) findViewById(R.id.autocomplete_course);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_courses, aCourses);
    search.setAdapter(adapter);

    showInfoButton = (Button) this.findViewById(R.id.show_info_button);
    showInfoButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (aCourses.toString().contains(search.getText()) == false ) {
                courseInfo.setText("Please select a course from the drop-down menu and click on the \"Show course information\" button");
            }
            else{
                String selectedCourse = search.toString();
                DataBase db1 = new DataBase(baseContext);
                db1.openDataBase();
                ArrayList<String> courseAttributes = db1.getCourseAttributes();
                ArrayList<String> attributeValues = db1.getAttributeValues(selectedCourse);
                db1.close();
                String courseInformation = "";
                for (int i=0; i < courseAttributes.size(); i++){
                    courseInformation = courseInformation + courseAttributes.get(i) + ": " + attributeValues.get(i) + System.getProperty("line.separator");
                }
                courseInfo.setText(courseInformation);
            }
        }
    });
}

问题来自

DataBase db1 = new DataBase(baseContext);

我尝试将其更改为

DataBase db = new DataBase(this.getApplicationContext());

and

DataBase db = new DataBase(null);

以及我尝试的每种方式,程序确实运行,但是当它处于 else 情况时,它会抛出错误并关闭。谁能告诉我应该使用什么上下文才能使其运行?

I am basically trying to get information from an sqlite db in another class. I've done this without any problems inside the onCreate method, but when I try to use it inside an onClickListener which is inside that onCreate I cannot find the right context to use. My code is as follows:

private AutoCompleteTextView search;
private Button showInfoButton;
private TextView courseInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_courses);
    final Context baseContext = getBaseContext();
    courseInfo = (TextView) this.findViewById(R.id.text);
    DataBase db = new DataBase(this.getApplicationContext());
    db.openDataBase();
    final ArrayList<String> aCourses = db.getCoursesArr();
    db.close();

    search = (AutoCompleteTextView) findViewById(R.id.autocomplete_course);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_courses, aCourses);
    search.setAdapter(adapter);

    showInfoButton = (Button) this.findViewById(R.id.show_info_button);
    showInfoButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (aCourses.toString().contains(search.getText()) == false ) {
                courseInfo.setText("Please select a course from the drop-down menu and click on the \"Show course information\" button");
            }
            else{
                String selectedCourse = search.toString();
                DataBase db1 = new DataBase(baseContext);
                db1.openDataBase();
                ArrayList<String> courseAttributes = db1.getCourseAttributes();
                ArrayList<String> attributeValues = db1.getAttributeValues(selectedCourse);
                db1.close();
                String courseInformation = "";
                for (int i=0; i < courseAttributes.size(); i++){
                    courseInformation = courseInformation + courseAttributes.get(i) + ": " + attributeValues.get(i) + System.getProperty("line.separator");
                }
                courseInfo.setText(courseInformation);
            }
        }
    });
}

the problem comes with

DataBase db1 = new DataBase(baseContext);

I've tried changing it to

DataBase db = new DataBase(this.getApplicationContext());

and

DataBase db = new DataBase(null);

and every way I try it, the program does run, but when it get's in the else case it throws an error and shuts down. Could anyone tell me what context should I use in order to make it run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-10-14 22:36:43

由于您可能多次需要数据库连接,因此我建议在您自己的扩展 Application 的应用程序类中打开和关闭数据库连接。

那里有 onCreate 和 onDestroy 方法,您可以在其中打开和关闭数据库(因为它有上下文)。如果您将数据库对象作为类变量(公共静态),您应该能够像 MyApplication.mDatabase 一样使用它。

As you may need the database connection more than once, I recommend to open and close the database connection in your own application class which extends Application.

There you have onCreate and onDestroy methods where you can open and close the database (because it has a context). If you make the database object as a class variable (public static) you should be able to use this like MyApplication.mDatabase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文