2013-11-15 72 views
0

我正在關注@JuanSánchez建議的answer,但我遇到了一個問題。Cursor.getType()API <11問題

當試圖將android.database.Cursor對象(c)轉換爲CursorWrapper時,我的問題是ClassCastException。更具體的代碼行如此CursorWrapper cw = (CursorWrapper)c;

 CursorWindow cursorWindow = null; 
     int cursorPosition = 0; 
     if (android.os.Build.VERSION.SDK_INT < 11) { 
      CursorWrapper cw = (CursorWrapper)c; 
      Class<?> cursorWrapper = CursorWrapper.class; 
      Field mCursor = null; 
      try { 
       mCursor = cursorWrapper.getDeclaredField("mCursor"); 
       mCursor.setAccessible(true); 
       AbstractWindowedCursor abstractWindowedCursor = (AbstractWindowedCursor)mCursor.get(cw); 
       cursorWindow = abstractWindowedCursor.getWindow(); 
       cursorPosition = abstractWindowedCursor.getPosition(); 
      } catch (NoSuchFieldException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalAccessException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

任何幫助,我將非常感激。

回答

0

解決了它。超級簡單。

換掉以下幾行。

交換CursorWrapper cw = (CursorWrapper)cursor;

對於CursorWrapper cw = new CursorWrapper(cursor);

+0

你也可以做'CursorWindow cursorWindow =((AbstractWindowedCursor)光標).getWindow();'。 – JAB