2015-11-03 116 views
0

問題是數據庫查詢出現了一些問題。我首先檢查它是否存在於數據庫中。如果它存在,那麼我從數據庫中獲取數據後就開始了一個意圖。如果它不存在,那麼它從網絡獲取,並把數據庫Sqllite數據庫查詢在獲取時遇到一些問題

這就是我所謂的數據庫funtion

protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    urlBlogtitle = mainBlogPost.get(position).title; // getting title of the clicked item 
    Log.d(TAG, urlBlogtitle); 
    summarygetter=databaseChecker(urlBlogtitle); 
    if(summarygetter=="") new DownloadXmlTaskContent(MainListActivity.this).execute(URL); 
    else startingIntent(summarygetter); 
    // intitating the main object for Content parsing 
} 

這是我databaseChecker方法是

private String databaseChecker(String title) { 

    DatabaseHandler db = new DatabaseHandler(MainListActivity.this); 

    String content=" "; 
    content = db.getContact(title); 
    Log.d(TAG,content); 
    return content; 
} 

這是我的數據庫方法

String getContact(String title) { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    String content = " "; 
    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, 
        KEY_TITLE, KEY_CONTENT }, KEY_ID + "=?", 
      new String[] { title }, null, null, null, null); 
    if (cursor != null){ 
     cursor.moveToFirst();} 
    else 
    { 
     return content; 
    } 

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), 
      cursor.getString(1), cursor.getString(2)); 
    content=contact.title; 
    // return contact 
    return content; 
} 

這是錯誤的logcat視圖。

11-04 00:56:32.803 2560-2560/com.example.talha.appforblog E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.example.talha.appforblog, PID: 2560 
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426) 
     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 
     at com.example.talha.appforblog.DatabaseHandler.getContact(DatabaseHandler.java:82) 
     at com.example.talha.appforblog.MainListActivity.databaseChecker(MainListActivity.java:102) 
     at com.example.talha.appforblog.MainListActivity.onListItemClick(MainListActivity.java:91) 
     at android.app.ListActivity$2.onItemClick(ListActivity.java:319) 
     at android.widget.AdapterView.performItemClick(AdapterView.java:300) 
     at android.widget.AbsListView.performItemClick(AbsListView.java:1143) 
     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044) 
     at android.widget.AbsListView$3.run(AbsListView.java:3833) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5221) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

回答

0

的問題是,你不檢查cursor.moveToFirst()返回值,如果沒有條目,將返回false。取而代之的是:

if (cursor != null){ 
    cursor.moveToFirst();} 
else 
{ 
    return content; 
} 

Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), 
     cursor.getString(1), cursor.getString(2)); 
content=contact.title; 
// return contact 
return content; 

試試這個:

if (cursor.moveToFirst()){ 
    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), 
     cursor.getString(1), cursor.getString(2)); 
    content=contact.title; 
    // return contact 
    return content; 
} else{ 
    return "Whatever_you_return_when_not_found" 
}