2017-03-06 56 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