我正在尝试从Cursor获取结果并将它们放入我创建的Object中。在Eclipse中,似乎光标返回一行(这是我想要的)但是当我尝试访问它时,我收到一个错误。这是获取单行的代码:
public Object fetchIssue(long IssueNumber, int type){
//Get query cursor.
Cursor cursor = mDB.query(true, ISSUES_TABLE, ISSUES_COLUMN_ARRAY, COL_ISSUE_ID + "=" + IssueNumber, null, null, null, null, null);
int iCount = cursor.getCount();
if(cursor == null)
return null;
Object objOut = null;
if(type == QUERY_TYPE_STRING_ARRAY){
//Create Array to hold Issue Information.
String[] IssueInfo = new String[3];
IssueInfo[0] = String.valueOf(IssueNumber);
IssueInfo[1] = cursor.getString(cursor.getColumnIndexOrThrow(COL_DATE_RECEIVED));
IssueInfo[2] = cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_SUMMARY));
IssueInfo[3] = cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_STATUS));
IssueInfo[4] = cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_SUB_STATUS));
IssueInfo[5] = cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_DESCRIPTION));
objOut = IssueInfo;
}
else{
//Create IssueInfo object.
IssueInfo IssueInfo = new IssueInfo(
IssueNumber, "12/12/2000",
//cursor.getString(cursor.getColumnIndexOrThrow(COL_DATE_RECEIVED)),
cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_SUMMARY)),
cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_STATUS)),
cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_SUB_STATUS)),
cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_DESCRIPTION)));
objOut = IssueInfo;
}
//Close cursor.
cursor.close();
//Return IssueInfo.
return objOut;
}
iCount返回1,如果我将游标变量放在“变量”窗口中,我可以看到所有列:
并且在类的顶部,表列变量设置为相同的名称:
public static final String ISSUES_TABLE = "Issues";
public static final String COL_ISSUE_ID = "_id";
public static final String COL_DATE_RECEIVED = "DateReceived";
public static final String COL_ISSUE_STATUS = "IssueStatus";
public static final String COL_ISSUE_SUB_STATUS = "IssueSubStatus";
public static final String COL_ISSUE_SUMMARY = "IssueSummary";
public static final String COL_ISSUE_DESCRIPTION = "IssueDescription";
private static final String[] ISSUES_COLUMN_ARRAY = new String[]{
COL_ISSUE_ID, COL_DATE_RECEIVED, COL_ISSUE_SUMMARY,
COL_ISSUE_STATUS, COL_ISSUE_SUB_STATUS, COL_ISSUE_DESCRIPTION};
此行发生错误:
//Create IssueInfo object.
IssueInfo IssueInfo = new IssueInfo(
IssueNumber, "12/12/2000",
//cursor.getString(cursor.getColumnIndexOrThrow(COL_DATE_RECEIVED)),
cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_SUMMARY)),
cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_STATUS)),
cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_SUB_STATUS)),
cursor.getString(cursor.getColumnIndexOrThrow(COL_ISSUE_DESCRIPTION)));
或其中任何一行(如您所见,我注释掉了收到的日期并在其中放置了一个正常的字符串值,它需要)。
这是错误:
03-13 09:06:42.370: E/AndroidRuntime(2324): FATAL EXCEPTION: AsyncTask #2
03-13 09:06:42.370: E/AndroidRuntime(2324): java.lang.RuntimeException: An error occured while executing doInBackground()
03-13 09:06:42.370: E/AndroidRuntime(2324): at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-13 09:06:42.370: E/AndroidRuntime(2324): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-13 09:06:42.370: E/AndroidRuntime(2324): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-13 09:06:42.370: E/AndroidRuntime(2324): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-13 09:06:42.370: E/AndroidRuntime(2324): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-13 09:06:42.370: E/AndroidRuntime(2324): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-13 09:06:42.370: E/AndroidRuntime(2324): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-13 09:06:42.370: E/AndroidRuntime(2324): at java.lang.Thread.run(Thread.java:856)
03-13 09:06:42.370: E/AndroidRuntime(2324): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
03-13 09:06:42.370: E/AndroidRuntime(2324): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
03-13 09:06:42.370: E/AndroidRuntime(2324): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
03-13 09:06:42.370: E/AndroidRuntime(2324): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
03-13 09:06:42.370: E/AndroidRuntime(2324): at com.adacel.dashboard.DBAdapter.fetchIssue(DBAdapter.java:215)
03-13 09:06:42.370: E/AndroidRuntime(2324): at com.adacel.dashboard.IssueInfoCreator.fetchIssue(IssueInfoCreator.java:42)
03-13 09:06:42.370: E/AndroidRuntime(2324): at com.adacel.dashboard.ViewIssue.LoadIssueDetails(ViewIssue.java:73)
03-13 09:06:42.370: E/AndroidRuntime(2324): at com.adacel.dashboard.ViewIssue.access$0(ViewIssue.java:70)
03-13 09:06:42.370: E/AndroidRuntime(2324): at com.adacel.dashboard.ViewIssue$GetIssueDetailsTask.doInBackground(ViewIssue.java:96)
03-13 09:06:42.370: E/AndroidRuntime(2324): at com.adacel.dashboard.ViewIssue$GetIssueDetailsTask.doInBackground(ViewIssue.java:1)
03-13 09:06:42.370: E/AndroidRuntime(2324): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-13 09:06:42.370: E/AndroidRuntime(2324): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-13 09:06:42.370: E/AndroidRuntime(2324): ... 4 more
非常感谢任何帮助!
答案 0 :(得分:3)
您必须先移动到光标的第一个索引:
cursor.moveToFirst();
答案 1 :(得分:0)
尝试修改IssueInfo的大小:
String[] IssueInfo = new String[3];
到
String[] IssueInfo = new String[6];
并将对象IssueInfo的名称更改为其他类似issue_info
答案 2 :(得分:0)
在查询之后,始终将光标移动到元素:
if(cursor == null)
return null;
if(!cursor.moveToFirst()) //this will move the cursor to the first element
return null;
如果您期望多行,则更好的方法是:
if(cursor != null) {
cursor.move(-1);
while(cursor.moveToNext()) {
//do stuff
}
}