在方法中使用正确的上下文来获取 sqlite 数据库
我基本上试图从另一个类中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您可能多次需要数据库连接,因此我建议在您自己的扩展
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
.