类型 Service 未定义方法 startManagingCursor(Cursor)?
我创建了一项服务。现在我试图通过创建数据库类的对象来访问服务的 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);
。
I have created one service. Now I am trying to access database in onstart() of service by creating boject of database class.i want to select records from some table,for that i used cursor.when i write startManagingCursor(cursor object) i occurring error there as method startManagingCursor(cursor object) is undefined for type srvice. now if i want to move cursor or manage it then how can i select records from that table? or is it not necessary to write startManagingCursor(cursor object); in service? if i remove this function then would i get records?here i have attached code:
@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();
}`
The error occurs at startManagingCursor(cNames);
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在服务中使用 startManagingcursor。托管游标负责在活动被销毁时关闭游标,并且当活动停止和重新启动时,它们将被停用并重新查询。在服务中这是不可能的。为了更好地理解验证这些答案
startManagingCursor() 在服务中?
和
startManagingCursor 的目的是什么?
没有 startmanaging 光标,您可以获得记录。但完成后您需要处理光标的关闭。 (cNames.close())
当您获取适配器时尝试
DBAdapter dbAdapter1 = DBAdapter.getDBAdapterInstance(getApplicationContext());
You cannot use startManagingcursor in a service. Managed cursors take care of closing the cursor when the activity is destroyed, and they will be deactivated and requeried as the activities is stopped and restarted. in a service this is not possible. for better understanding verify these answers
startManagingCursor() in a service?
and
What's the purpose of startManagingCursor?
without startmanaging cursor you can get the records. but you need to handle the closing of the cursors when you finish. (cNames.close())
and when your getting the adapter try
DBAdapter dbAdapter1 = DBAdapter.getDBAdapterInstance(getApplicationContext());