查询没有记录的内容提供商?
我正在研究一种使用游标查询内容提供者的方法。删除记录后,它会调用 loadfromProvider
方法并刷新数组列表。内容提供程序通常包含记录,但是,当我删除所有记录并且查询自动运行时,它会引发异常。这是我的方法:
private void loadFromProvider() {
// Clear the existing array list
EQlist.clear();
ContentResolver cr = getContentResolver();
// Return all the saved records
Cursor c = cr.query(EQProvider.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
do {
String details = c.getString(EQProvider.DETAILS_COLUMN);
String linkString = c.getString(EQProvider.LINK_COLUMN);
EQli q = new EQli(details, linkString);
addEQToArray(q);
} while(c.moveToNext());
}
c.close();
}
当我在内容提供程序中没有记录的情况下运行此方法时,它会抛出以下内容:
java.lang.IndexOutOfBoundsException:无效索引 1,大小为 1 在 java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
我认为这是由于光标试图解析空值。我试图找出一种方法,如果光标没有返回任何记录,它会绕过代码的其余部分并且不会发生任何事情。
任何帮助将不胜感激。谢谢
I am working on a method that queries a content provider using a cursor. After I delete a record, it calls the loadfromProvider
method and refreshes the arraylist. The content provider normally has records in it however, when I delete all the records and the query runs automatically it throws exceptions. Here is my method:
private void loadFromProvider() {
// Clear the existing array list
EQlist.clear();
ContentResolver cr = getContentResolver();
// Return all the saved records
Cursor c = cr.query(EQProvider.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
do {
String details = c.getString(EQProvider.DETAILS_COLUMN);
String linkString = c.getString(EQProvider.LINK_COLUMN);
EQli q = new EQli(details, linkString);
addEQToArray(q);
} while(c.moveToNext());
}
c.close();
}
When I run this with no records in the content provider it throws the following:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
I think this is due to the cursor trying to parse a null value. I am trying to figure out a way that if the cursor does not come back with any records, it bypasses the rest of the code and does nothing happens.
Any help would be appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有记录,您的光标应该为空。如果光标为空,则
c.moveToFirst()
将返回false
并且您不会进入 do-while 循环。调试您的应用程序并确保光标为空。
If there are no records your cursor should be empty. If the cursor is empty then
c.moveToFirst()
will returnfalse
and you are not going to enter the do-while loop.Debug your application and make sure that the cursor is empty.