在非 activity class 比较器中获取上下文
Getting context in non-activity class comparator
我必须从我的一个实体 class 的比较器 class 访问我的数据库。
为了实例化数据库连接,我需要一个应用程序上下文。
在这种情况下如何获取应用程序上下文:
实体 - class 带有比较器
static public class CowLastInspectionComparator implements Comparator<Cow> {
public int compare(Cow cow1, Cow cow2) {
DbAdapter adapter = DbAdapter.getInstance(new MainView().getApplicationContext()); //this doesn't work
List<Inspection> inspectionsCow1 = adapter.getInspectionByCow(cow1);
List<Inspection> inspectionsCow2 = adapter.getInspectionByCow(cow2);
return inspectionsCow1.get(0).getJob().getJobDate().compareTo(inspectionsCow2.get(0).getJob().getJobDate());
}
}
在 Activity 中排序:
if (sortOption == "Name") Collections.sort(cows, new Cow.CowLastInspectionComparator());
I have to access my database from the comparator class from one of my entity-classes
如果您的意思是要在 compare()
中执行数据库 I/O,那将使您的 compare()
调用变慢,而您的 sort()
调用 非常慢。如果你想对你的,嗯,奶牛进行排序,但它们并不都在内存中,请使用 ORDER BY
子句执行 single 数据库查询以正确排序它们。
For instancing the database connection I need an application context.
您需要 Context
。它不一定必须是 Application
.
How do I get the application context in this case
向 CowLastInspectionComparator
提供一个,例如通过构造函数参数。
static public class CowLastInspectionComparator implements Comparator<Cow> {
private Context context;
CowLastInspectionComparator(Context context){
this.context=context;
}
public int compare(Cow cow1, Cow cow2) {
DbAdapter adapter = DbAdapter.getInstance(context); //this doesn't work
List<Inspection> inspectionsCow1 = adapter.getInspectionByCow(cow1);
List<Inspection> inspectionsCow2 = adapter.getInspectionByCow(cow2);
return inspectionsCow1.get(0).getJob().getJobDate().compareTo(inspectionsCow2.get(0).getJob().getJobDate());
}
}
在Activity中:
if (sortOption == "Name"){
Cow.CowLastInspectionComparator comparator=new Cow.CowLastInspectionComparator(this);
Collections.sort(cows,comparator);
}
我必须从我的一个实体 class 的比较器 class 访问我的数据库。 为了实例化数据库连接,我需要一个应用程序上下文。
在这种情况下如何获取应用程序上下文:
实体 - class 带有比较器
static public class CowLastInspectionComparator implements Comparator<Cow> {
public int compare(Cow cow1, Cow cow2) {
DbAdapter adapter = DbAdapter.getInstance(new MainView().getApplicationContext()); //this doesn't work
List<Inspection> inspectionsCow1 = adapter.getInspectionByCow(cow1);
List<Inspection> inspectionsCow2 = adapter.getInspectionByCow(cow2);
return inspectionsCow1.get(0).getJob().getJobDate().compareTo(inspectionsCow2.get(0).getJob().getJobDate());
}
}
在 Activity 中排序:
if (sortOption == "Name") Collections.sort(cows, new Cow.CowLastInspectionComparator());
I have to access my database from the comparator class from one of my entity-classes
如果您的意思是要在 compare()
中执行数据库 I/O,那将使您的 compare()
调用变慢,而您的 sort()
调用 非常慢。如果你想对你的,嗯,奶牛进行排序,但它们并不都在内存中,请使用 ORDER BY
子句执行 single 数据库查询以正确排序它们。
For instancing the database connection I need an application context.
您需要 Context
。它不一定必须是 Application
.
How do I get the application context in this case
向 CowLastInspectionComparator
提供一个,例如通过构造函数参数。
static public class CowLastInspectionComparator implements Comparator<Cow> {
private Context context;
CowLastInspectionComparator(Context context){
this.context=context;
}
public int compare(Cow cow1, Cow cow2) {
DbAdapter adapter = DbAdapter.getInstance(context); //this doesn't work
List<Inspection> inspectionsCow1 = adapter.getInspectionByCow(cow1);
List<Inspection> inspectionsCow2 = adapter.getInspectionByCow(cow2);
return inspectionsCow1.get(0).getJob().getJobDate().compareTo(inspectionsCow2.get(0).getJob().getJobDate());
}
}
在Activity中:
if (sortOption == "Name"){
Cow.CowLastInspectionComparator comparator=new Cow.CowLastInspectionComparator(this);
Collections.sort(cows,comparator);
}