我想调用Other类中的方法,但是我在这一行得到了错误:
dbh=new DatabaseHelper(getApplicationContext());
错误讯息:
“类型add_point”
未定义方法getApplicationContext()
答案 0 :(得分:0)
如果您计划在某些非activity
类中使用该帮助程序,我建议您使用setter或特定构造函数传递Context
一次,并使Helper保持静态。 非活动/服务类不了解applicationContext
。
private static Context context;
public static void setContext(Context context){
DatabaseHelper.context = context;
}
你也可以使用Singleton
模式来拥有一个帮助者的单个实例。
答案 1 :(得分:0)
我有类databsehelper,但是在新类show errore中调用methode。
public class add_point extends TestCase {
SQLiteDatabase db;
DatabaseHelper dbh;
final long ONE_DAY = 24 * 60 * 60 * 1000L;
Calendar cal = Calendar.getInstance();
Hijri hij= new Hijri();
Monasebat mon=new Monasebat();
JalaliCalendar jl=new JalaliCalendar();
String event;
Date today = new Date();
dateConvert_Shamsi dshamsi =new dateConvert_Shamsi();
String text;
String DateFirst= "03/20/2016";
String DateEnde= "04/22/2016";
String df="03/20/2016";
String ef="04/22/2016";//42";
private ProgressDialog progressDialog;
private ProgressBar bar;
public void start2()
{
dbh=new DatabaseHelper(getApplicationContext());
//dbh=new DatabaseHelper(getApplicationContext());
for(int i=0;i<13;i++)
{
try
{
switch (i) {
case 1:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"روزکار","روزکار","روزکار","روزکار","روزکار","روزکار","روزکار");
break;
case 2:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"فولادمبارکه","شیفت","A","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 3:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"فولادمبارکه","شیفت","B","استراحت اول","استراحت دوم","روزکار","شب کار");
break;
case 4:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"فولادمبارکه","شیفت","C","شب کار","استراحت اول","استراحت دوم","روزکار");
break;
case 5:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"فولادمبارکه","شیفت","D","استراحت دوم","روزکار","شب کار","استراحت اول");
break;
case 6:
//getDatesBetween(Dat"04/22/2016"irst,DateEnde,"tbl_Addshift_"+String.valueOf(i),"فولادمبارکه","شیفت","نوبت کار","شیفت","شب کار","استراحت اول","استراحت دوم");
break;
/*case 7:
getDatesBetween("03/20/2016",ef,"tbl_Addshift_"+String.valueOf(i),"ذوب آهن","روزکار","روزکار","روزکار","روزکار","روزکار","روزکار");
break;*/
case 7:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"ذوب آهن","شیفت","1","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 8:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"ذوب آهن","شیفت","2","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 9:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"ذوب آهن","شیفت","3","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 10:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"ذوب آهن","شیفت","4","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 11:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"فولادمبارکه","شیفت","1نوبت کار","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 12:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"فولادمبارکه","شیفت","2نوبت کار","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 13:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_"+String.valueOf(i),"روزکار","روزکار","روزکار","روزکار","روزکار","روزکار","روزکار");
break;
}
}
catch (Exception e)
{
System.out.println(e+"--------------start2--------------");
}
}
}
public void getDatesBetween(String startDate,String endDate,String tbl,String nameWork,String typeshift,String nameShift,String day1,String day2,String day3,String day4) {
dbh=new DatabaseHelper(getApplicationContext());
int DayShamsi = 0;
int MountShamsi;
int YearShamsi;
String txt_day_count="";
//Calendar mydate = Calendar.getInstance();
String DateJalali;
String DateJalali2 = null;
String DateJalali3 = null;
String datetime="";
try
{
long from=Date.parse(startDate);
long to=Date.parse(endDate);
int x=0;
int cont=1;
while(from <= to) {
x=x+1;
if (cont==1)
{
txt_day_count=day1;
cont++;
}
else if (cont==2)
{
txt_day_count=day2;
cont++;
}
else if(cont==3)
{
txt_day_count=day3;
cont++;
}
else if(cont==4)
{
txt_day_count=day4;
cont=1;
}
cal.setTime(new Date(from));
DateJalali=jl.getJalaliDate(new Date(from));
dshamsi.setGregorianDate(cal.get(cal.YEAR),cal.get(cal.MONTH)+1,cal.get(cal.DAY_OF_MONTH));
event=mon.JalaliMonasebat(cal.get(cal.MONTH)+1,cal.get(cal.DAY_OF_MONTH));
hij.miladi(new Date(from));
DateJalali2=hij.Dayhejri();
DateJalali3=String.valueOf(hij.JalaliTatil()+"\n"+mon.JalaliTatil());
int tatile=0;
int tatileH=0;
tatile = mon.JalaliTatil();
tatileH = hij.JalaliTatil();
dbh.Add_Shift(datetime,String.valueOf(cal.getTime()),
nameWork,
typeshift,
nameShift,
txt_day_count,
String.valueOf(dshamsi.getIranianYear()+"-"+dshamsi.getIranianMonth()),//String.valueOf(jl.JalaliYear(cal.getTime()))+"-"+String.valueOf(jl.JalaliMonth(cal.getTime())),//DateJalali3+"-"+DateJalali2,// String.valueOf(YearShamsi+"-"+MountShamsi),
String.valueOf(new Date(from)),
event+"\n"+DateJalali2,
DateJalali3,
tbl,
String.valueOf(tatile+"-"+tatileH)
);
System.out.println(String.valueOf(new Date(from))+"-----------getDatesBetween-----------------");
from += ONE_DAY;
}
}
catch (Exception e)
{
System.out.println(e+"-----------0000-getDatesBetween---------------- "+"\n"+String.valueOf(datetime+cal.getTime().toString()+"-"+
nameWork+"-"+
typeshift+"-"+
nameShift+"-"+
txt_day_count+"-"+
String.valueOf(dshamsi.getIranianYear()+"-"+dshamsi.getIranianMonth())+"-"+//String.valueOf(jl.JalaliYear(cal.getTime()))+"-"+String.valueOf(jl.JalaliMonth(cal.getTime())),//DateJalali3+"-"+DateJalali2,// String.valueOf(YearShamsi+"-"+MountShamsi),
String.valueOf(new Date(startDate))+"-"+
event+"\n"+DateJalali2+"-"+
DateJalali3+"-"+
tbl+
String.valueOf("tatile"+"-"+"tatileH")));
}
}
public void add()
{
dbh=new DatabaseHelper(getApplicationContext());
for(int i=0 ; i<12 ; i++)
{
try
{
switch (i) {
case 1:
dbh.AddRowtable("روزکار","روزکار","روزکار","tbl_Addshift_"+String.valueOf(i));
break;
case 2:
dbh.AddRowtable("فولادمبارکه","شیفت","A","tbl_Addshift_"+String.valueOf(i));
break;
case 3:
dbh.AddRowtable("فولادمبارکه","شیفت","B","tbl_Addshift_"+String.valueOf(i));
break;
case 4:
dbh.AddRowtable("فولادمبارکه","شیفت","C","tbl_Addshift_"+String.valueOf(i));
break;
case 5:
dbh.AddRowtable("فولادمبارکه","شیفت","D","tbl_Addshift_"+String.valueOf(i));
break;
case 6:
dbh.AddRowtable("فولادمبارکه","شیفت","نوبت کار","tbl_Addshift_"+String.valueOf(i));
break;
case 7:
dbh.AddRowtable("ذوب آهن","شیفت","1","tbl_Addshift_"+String.valueOf(i));
break;
case 8:
dbh.AddRowtable("ذوب آهن","شیفت","2","tbl_Addshift_"+String.valueOf(i));
break;
case 9:
dbh.AddRowtable("ذوب آهن","شیفت","3","tbl_Addshift_"+String.valueOf(i));
break;
case 10:
dbh.AddRowtable("ذوب آهن","شیفت","4","tbl_Addshift_"+String.valueOf(i));
break;
case 11:
dbh.AddRowtable("ذوب آهن","نوبت کار","1","tbl_Addshift_"+String.valueOf(i));
break;
case 12:
dbh.AddRowtable("ذوب آهن","نوبت کار","2","tbl_Addshift_"+String.valueOf(i));
break;
}
}
catch (Exception e)
{
System.out.println(e+"--------------add--------------");
}
//dbh.AddRowtable(txt_ID.getText().toString(), txt_name.getText().toString(), txt_family.getText().toString());
}
}
}
答案 2 :(得分:0)
如果使用Fragment则不支持getApplicationContext(),它只使用getActivity()。
以及使用活动请参阅此链接:
http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/