【问题标题】:How to setup "where" statement in android content provider query for media store如何在 android 内容提供商查询中为媒体商店设置“where”语句
【发布时间】:2012-05-26 20:51:29
【问题描述】:

我可以查询 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 语句不起作用...

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

【问题讨论】:

    标签: android android-contentprovider mediastore


    【解决方案1】:

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

    试试这个

     if (c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM)).equalsIgnoreCase(selection)) {
    
                            //do stuff with this data
    
                }
    

    【讨论】:

    • 感谢工作!但是,如果有不同的专辑、艺术家、播放列表......情况确实很重要,嗯......好吧。在我能够正确获取查询的“where”部分之前,我将不得不做出适当的处理。谢谢!
    • 如果我解决了您的问题,您可以验证我的回答。对于区分大小写,您可以使用 equals()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多