2016-02-01 27 views
-1

如何獲取內容提供者的每一行?我試圖做到這一點使用Cursor c = getContentResolver().query(uri, null, null, null, null) 然後如何獲取內容提供者的每一行?

String s; 
if (c != null && c.moveToFirst()) 

     while (c.moveToNext()) 
       s = c.getString(c.getColumnIndexOrThrow("string")); 

c.close(); 

但它沒有采取所有行不work.Instead,它是隻取最後一個,反覆,多次行-1我的分貝。

+0

「?我怎樣才能得到每一行從內容提供商」:

時對由DB支持的內容提供商運行此代碼對我的作品 - 這取決於「ContentProvider」的實現。 – CommonsWare

+0

我有一個擴展了ContentProvider的類。 – alfakaiwmega

+0

然後問題出現在你的ContentProvider實現中。你需要實現你的'ContentProvider',以便你想要的'query()'調用返回所有的行。 – CommonsWare

回答

0

由於使用moveToFirst()後跟moveToNext()作爲循環控件會導致您跳過第一行,因此您會得到「rows-1」。

如果您看到數據庫的所有行對於「字符串」列具有相同的值,則顯示「s」的代碼有問題(您沒有發佈),或者數據庫包含每行的值相同,或者您的ContentProvider中沒有正確實施query()

您還可以使用Cursor.getCount()來獲取遊標中的行數。

 Cursor c = getContentResolver().query(uri, null, null, null, null); 
     if (c != null) { 
      while (c.moveToNext()) { 
       String s = c.getString(c.getColumnIndexOrThrow("name")); 
       Log.i("Demo", s); 
      } 
      c.close(); 
     } 
+0

我認爲問題是在遊標迭代中產生的。我正在使用arraylist,每次在循環開始之前,我都會在arraylist中添加s值。當循環結束時,我放棄了它,並且arraylist的值是錯誤的。但記錄的值很好! – alfakaiwmega

+0

arraylist只有最後一個值重複行時間 – alfakaiwmega

+0

我發現了錯誤!我正在將字符串初始化爲循環! – alfakaiwmega