【问题标题】:method startManagingCursor(Cursor) is undefined for the type Service?方法 startManagingCursor(Cursor) 未为服务类型定义?
【发布时间】:2011-11-14 05:51:56
【问题描述】:

我创建了一项服务。现在我正在尝试通过创建数据库类的对象来访问服务的 onstart() 中的数据库。我想从某个表中选择记录,因为我使用了游标。当我写 startManagingCursor(cursor object) 时,我在那里作为方法 startManagingCursor 发生错误(光标对象)对于类型服务未定义。现在,如果我想移动光标或管理它,那么如何从该表中选择记录?还是不需要写 startManagingCursor(cursor object);在服务中?如果我删除这个函数,我会得到记录吗?这里我附上了代码:

       @Override
   public void onStart(Intent intent, int startid)
   {
       DBAdapter dbAdapter1 = DBAdapter.getDBAdapterInstance(Srvc_Sms_email.this);
       dbAdapter1.openDataBase();

        String[] sel = {"pid","date","datename"};
        Cursor cNames = dbAdapter1.selectRecordsFromDB("datesdatabase",sel,null,null,null,null,null);
        startManagingCursor(cNames);
        cNames.moveToFirst();
        int i1 =0;
        while (cNames.isAfterLast() == false)
        {
            pid.add(cNames.getInt(0));    
            datelist.add(cNames.getString(1));
            namelist.add(cNames.getString(2));
            cNames.moveToNext();
        }`

错误发生在startManagingCursor(cNames);

【问题讨论】:

标签: android android-service android-cursor


【解决方案1】:

您不能在服务中使用 startManagingcursor。托管游标负责在活动被销毁时关闭游标,并且在活动停止和重新启动时它们将被停用和重新查询。在服务中这是不可能的。为了更好地理解,请验证这些答案 startManagingCursor() in a service?

What's the purpose of startManagingCursor?

无需启动游标即可获取记录。但是您需要在完成后处理光标的关闭。 (cNames.close())

当你得到适配器时尝试

DBAdapter dbAdapter1 = DBAdapter.getDBAdapterInstance(getApplicationContext());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2022-11-03
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    相关资源
    最近更新 更多