2017-03-06 60 views
0

獲得使用SQLite查詢吸走,這裏是我的問題,採取MediaStore.Audio.Artist.Artist爲例,Android的ContentProvider的查詢條件問題

String[] projection={MediaStore.Audio.Artists._ID,MediaStore.Audio.Artists.ARTIST}; 
String selectionClause=MediaStore.Audio.Artists.ARTIST+" = ?"; 
String[] selectionArgs={"Artist1","Artist2"}; 

只想拿回匹配字符串「Artist1」的結果,並「Artist2」,但我得到像下面的錯誤,

Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 1 parameters. 

請告訴我這是什麼錯誤,正確的方法來做,謝謝!

回答

0

感謝Emanue和CL,立足自己的想法,下面是解決方案,

MediaStore.Audio.Artists.ARTIST+" = ? OR " + MediaStore.Audio.Artists.ARTIST+" = ?"; 

正如你所看到的,在這裏你應該用「OR」而不是「AND」,如果想要查詢的結果將匹配selectionArgs。 PS。 selectionArgs項的數量應該與selectionClause的數量相同。

再次感謝大家!

0

你需要儘可能多的'?'在子句參數中,您將在參數中使用。 在這種情況下,它會是這樣的:

MediaStore.Audio.Artists.ARTIST+" = ? AND " + MediaStore.Audio.Artists.ARTIST+" = ?"; 
+0

感謝您的回答,但不幸的是跟着你回答,cursoradapter不會被調用,不知道爲什麼? – Leon

+0

我不確定你的意思是「不被稱爲」。請確保您的查詢將返回有效的結果,然後逐步調試 –

+0

抱歉,我的表達不是很清楚,我的意思是當使用ur的想法時,CursorAdapter的方法bindView將不會執行。我在bindView中設置了Log.e命令,並且發現Log.e未運行 – Leon