大家都知道我是Android的新手,这个应用程序是我第一个使用数据库的应用程序。在我的应用程序中,我有DatabaseHandler
类这个简单的结构
public class DatabaseHandler extends SQLiteOpenHelper{
private static String DB_PATH = "";
private static final String DATABASE_NAME = "tsms";
private static String RECEIVE_FIELDS_TABLE = "ReceiveFields";
private static final String COLUMN_ID = "id";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
现在在名为TSMS
的其他课程中,我可以将字段ReceivedFields
填充为:
public class ReceiveFields {
public long lastId;
public String smsNumber;
public String mobileNumber;
public String senderName;
public String smsBody;
public DateTimeD1 receiveDate;
public ReceiveFields(){
}
public ReceiveFields(long lastId, String smsNumber, String mobileNumber, String senderName, String smsBody, DateTimeD1 receiveDate) {
this.lastId = lastId;
this.smsNumber = smsNumber;
this.mobileNumber = mobileNumber;
this.senderName = senderName;
this.smsBody = smsBody;
this.receiveDate = receiveDate;
}
TSMS
类函数填充没有任何错误,我想将其保存到数据库:
for (int i = 0; i <= strings.length - 1; i++) {
String[] str1 = WSDLHelper.convert3(strings[i]);
try {
receiveArray.add(new ReceiveFields(
Long.valueOf(str1[0]),
str1[1],
str1[2],
URLDecoder.decode(str1[3], "UTF-8"),
URLDecoder.decode(str1[4], "UTF-8"),
WSDLHelper.convertDate(str1[5])));
}
catch (UnsupportedEncodingException ex) {
throw new TException(PublicErrorList.NOT_EXIST_ERROR_DETAIL);
}
}
现在我想将记录保存到数据库for
我之前定义DatabaseHandler db = new DatabaseHandler(this);
所以我得到了这个错误,我无法解决这个问题:
答案 0 :(得分:3)
您必须将上下文传递给DatabaseHandler类。所以你可以使用Activity,Service或Broadcast Receiver来完成它。所以改变这个构造函数
public ReceiveFields(){
}
到
public ReceiveFields(Context ctx){
this.ctx=ctx; //create a field Context ctx;
}
从活动,服务或接收方传递上下文,您可以使用ctx
代替this
。