Android的使用数据库处理器出活动 [英] Android use DatabaseHandler out of Activity

查看:217
本文介绍了Android的使用数据库处理器出活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我是在Android的新的和这个应用程序是我使用的数据库第一个应用程序。在我的应用我有数据库处理器这个简单的结构类

Hi all i'm new in Android and this application is my first app for use database. in my application i have DatabaseHandler class with this simple structure

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 为:

now in other class as name TSMS i can fill Fields ReceivedFields as :

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 类函数来填充没有任何错误,我想将其保存到数据库:

TSMS class function to fill without any error and i want to save it to database:

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);
                }
            }

现在我想记录到数据库保存到我敢确定数据库处理器DB =新的数据库处理器(本); 在此之前,让我得到这个错误,我无法解析:

now i want to save records to database into for i'm define DatabaseHandler db = new DatabaseHandler(this); before that so i'm getting this error and i can not resolve that:

推荐答案

您必须通过上下文数据库处理器类。所以,你可以带活动,服务和广播接收器做。因此,改变这种构造器

You have to pass context to DatabaseHandler class. So you can do it with Activity, Service or Broadcast Receiver. So change this contructor

public ReceiveFields(){

    }

 public ReceiveFields(Context ctx){
    this.ctx=ctx; //create a field Context ctx;
    }

妙传活动,服务或接收器方面,你可以使用 CTX 而不是这个

这篇关于Android的使用数据库处理器出活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆