0

我可以查询 Android MediaStore,但无法设置“where”语句。所以我放弃了尝试获取它并尝试破解它,我的破解也不起作用。

我的技巧是移动光标的每一行,看看我感兴趣的列的字符串值是否等同于我的搜索关键字......我的代码如下。我想做的是找到专辑(或艺术家、播放列表)中的所有歌曲。

为“where”或为什么我的字符串比较不起作用设置查询的任何帮助将不胜感激...

private void loadList() {

        String selection = ((HashMap<String, String>) selectedAlbumData.get(0))
                .get("Album").toString();

        String[] proj = { MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.DURATION };

        // managed query doesn't need startManagingCursor called on the
        Cursor c = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                proj, null, null, null);

        if (c.getCount() < 1) {
            Log.d("AFA", "Cursor query is empty.. :( ...");
        } else {

            // do stuff with our content...
            while (c.moveToNext()) {

                HashMap<String, String> temp = new HashMap<String, String>();

                if (c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM)) == selection) {

                            //do stuff with this data

                }
                Log.d("SongChoosing", "C str is: " + c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
            }

            //debug stuff
            c.moveToPosition(5);
            Log.d("SongChoosing", "Selection is: " + selection);
            Log.d("SongChoosing", "C str is: " + c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
        }
    }

我在专辑列中查找的字符串值是一个有效字符串,它不是 null 或空的。我尝试在 log cat 上打印我的歌曲的每一行,字符串看起来相当于肉眼,但我的 if 语句是不工作...

非常感谢任何可以帮助我的人。

4

1 回答 1

1

您必须使用 equals 或 equalsIgnoreCase 而不是 == 与字符串。

试试这个

 if (c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM)).equalsIgnoreCase(selection)) {

                        //do stuff with this data

            }
于 2012-05-26T21:08:04.467 回答