Example usage for android.widget SimpleCursorAdapter setFilterQueryProvider

List of usage examples for android.widget SimpleCursorAdapter setFilterQueryProvider

Introduction

In this page you can find the example usage for android.widget SimpleCursorAdapter setFilterQueryProvider.

Prototype

public void setFilterQueryProvider(FilterQueryProvider filterQueryProvider) 

Source Link

Document

Sets the query filter provider used to filter the current Cursor.

Usage

From source file:com.ultramegasoft.flavordex2.util.EntryFormHelper.java

/**
 * Set up the autocomplete for the maker field.
 *///  w  ww.j ava2 s  .co  m
private void setupMakersAutoComplete() {
    final SimpleCursorAdapter adapter = new SimpleCursorAdapter(mFragment.getContext(),
            R.layout.simple_dropdown_item_2line, null,
            new String[] { Tables.Makers.NAME, Tables.Makers.LOCATION },
            new int[] { android.R.id.text1, android.R.id.text2 }, 0);

    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence constraint) {
            final Uri uri;
            if (TextUtils.isEmpty(constraint)) {
                uri = Tables.Makers.CONTENT_URI;
            } else {
                uri = Uri.withAppendedPath(Tables.Makers.CONTENT_FILTER_URI_BASE,
                        Uri.encode(constraint.toString()));
            }

            final Bundle args = new Bundle();
            args.putParcelable("uri", uri);

            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mFragment.getLoaderManager().restartLoader(LOADER_MAKERS, args, EntryFormHelper.this);
                }
            });

            return adapter.getCursor();
        }
    });

    mTxtMaker.setAdapter(adapter);

    // fill in maker and origin fields with a suggestion
    mTxtMaker.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Cursor cursor = (Cursor) parent.getItemAtPosition(position);
            cursor.moveToPosition(position);

            final String name = cursor.getString(cursor.getColumnIndex(Tables.Makers.NAME));
            final String origin = cursor.getString(cursor.getColumnIndex(Tables.Makers.LOCATION));
            mTxtMaker.setText(name);
            mTxtOrigin.setText(origin);

            // skip origin field
            mTxtOrigin.focusSearch(View.FOCUS_DOWN).requestFocus();
        }
    });
}

From source file:com.xmobileapp.rockplayer.RockPlayer.java

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    /*/* w  w w  .j  a v a  2  s.  c om*/
     * Shuffle
     */
    case 0:
        if (this.SHUFFLE)
            this.SHUFFLE = false;
        else
            this.SHUFFLE = true;
        try {
            playerServiceIface.setShuffle(this.SHUFFLE);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        RockOnPreferenceManager settings = new RockOnPreferenceManager(FILEX_PREFERENCES_PATH);
        settings.putBoolean("Shuffle", this.SHUFFLE);
        return true;
    /*
     * Search
     */
    case 1:
        if (GRATIS) {
            showLitePopup();
            return true;
        }

        //this.hideMainUI();
        this.showSongSearch();
        this.songSearchTextView.requestFocus();
        Cursor allSongsCursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, SONG_COLS, // we should minimize the number of columns
                null, // all songs
                null, // parameters to the previous parameter - which is null also 
                null // sort order, SQLite-like
        );
        SimpleCursorAdapter songAdapter = new SimpleCursorAdapter(this, R.layout.simple_dropdown_item_2line,
                allSongsCursor, new String[] { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST },
                new int[] { R.id.text1, R.id.text2 });
        FilterQueryProvider songSearchFilterProvider = new FilterQueryProvider() {
            @Override
            public Cursor runQuery(CharSequence constraint) {
                String whereClause = MediaStore.Audio.Media.TITLE + " LIKE '%" + constraint + "%'" + " OR "
                        + MediaStore.Audio.Media.ARTIST + " LIKE '%" + constraint + "%'";
                Log.i("SEARCH", whereClause);
                //whereClause = null;
                Cursor songsCursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                        SONG_COLS, // we should minimize the number of columns
                        whereClause, // songs where the title or artist name matches
                        null, // parameters to the previous parameter - which is null also 
                        null // sort order, SQLite-like
                );
                return songsCursor;
            }
        };
        songAdapter.setFilterQueryProvider(songSearchFilterProvider);
        this.songSearchTextView.setAdapter(songAdapter);
        songAdapter
                .setStringConversionColumn(allSongsCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
        //this.songSearchTextView.setOnKeyListener(songSearchKeyListener);
        return true;
    /*
     * Share
     */
    case 5:
        if (GRATIS) {
            showLitePopup();
            return true;
        }

        ShareSong shareSong = new ShareSong(context, songCursor);
        shareSong.shareByMail();
        return true;
    /*
     * Get Art
     */
    case 3:
        //           if(GRATIS){
        //              showLitePopup();
        //              return true;
        //           }
        albumReloadProgressDialog = new ProgressDialog(this);
        albumReloadProgressDialog.setIcon(R.drawable.ic_menu_music_library);
        albumReloadProgressDialog.setTitle("Loading Album Art");
        albumReloadProgressDialog.setMessage("Waiting for Last.FM connection");
        // TODO: set powered by Last.FM
        albumReloadProgressDialog.show();
        Thread albumArtThread = new Thread() {
            public void run() {
                try {
                    LastFmAlbumArtImporter lastFmArtImporter = new LastFmAlbumArtImporter(context);
                    lastFmArtImporter.getAlbumArt();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        //albumArtThread.setUncaughtExceptionHandler(albumArtUncaughtExceptionHandler);
        albumArtThread.start();
        //containerLayout.addView(albumReloadProgressDialog);
        return true;
    /*
     * Concerts
     */
    case 4:
        if (GRATIS) {
            showLitePopup();
            return true;
        }

        this.hideMainUI();
        this.mainUIContainer.setVisibility(View.GONE);
        //this.hideHelpUI();
        this.showEventUI();

        /*
         * Concert Radius Thing 
         */
        // TODO: need a metric
        //         SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
        RockOnPreferenceManager prefs = new RockOnPreferenceManager(FILEX_PREFERENCES_PATH);
        concertRadius = prefs.getLong("ConcertRadius", (long) (this.CONCERT_RADIUS_DEFAULT));
        this.eventListRadius.setText(String.valueOf(Math.round((float) concertRadius / 1000)));

        /*
         * Show a dialog to give some nice feedback to the user
         */
        concertAnalysisProgressDialog = new ProgressDialog(this);
        concertAnalysisProgressDialog.setIcon(android.R.drawable.ic_menu_today);
        concertAnalysisProgressDialog.setTitle("Analysing concert information");
        concertAnalysisProgressDialog.setMessage("Waiting for Last.FM connection");
        concertAnalysisProgressDialog.show();

        /*
         * Analyze concert info
         */
        lastFmEventImporter = new LastFmEventImporter(context);
        new Thread() {
            public void run() {
                try {
                    lastFmEventImporter.getArtistEvents();
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        return true;
    /*
     * Playlists
     */
    case 6:
        if (GRATIS) {
            showLitePopup();
            return true;
        }

        /*
         * Get the views and make them visible
         */
        String sortOrder = MediaStore.Audio.Playlists.NAME + " ASC";
        Cursor playlistAllCursor = contentResolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
                this.PLAYLIST_COLS, null, null, sortOrder);

        /*
         * Create Array with custom playlist + system playlists
         */
        Playlist playlistTemp;
        ArrayList<Playlist> playlistArray = new ArrayList<Playlist>();
        /* ALL Playlist*/
        playlistTemp = new Playlist();
        playlistTemp.id = constants.PLAYLIST_ALL;
        playlistTemp.name = "All songs";
        playlistArray.add(playlistTemp);
        /* Recently Added */
        playlistTemp = new Playlist();
        playlistTemp.id = constants.PLAYLIST_RECENT;
        playlistTemp.name = "Recently Added";
        playlistArray.add(playlistTemp);
        /* ... other system playlists ... */

        /* add every playlist in the media store */
        while (playlistAllCursor.moveToNext()) {
            playlistTemp = new Playlist();
            playlistTemp.id = playlistAllCursor
                    .getLong(playlistAllCursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists._ID));
            playlistTemp.name = playlistAllCursor
                    .getString(playlistAllCursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.NAME));
            playlistArray.add(playlistTemp);
            Log.i("PLAYLIST MENU", playlistTemp.id + " " + playlistTemp.name);
        }

        //           String[] fieldsFrom = new String[1];
        //          int[]   fieldsTo = new int[1];
        //           fieldsFrom[0] = MediaStore.Audio.Playlists.NAME;
        //          fieldsTo[0] = R.id.playlist_name;
        //           PlaylistCursorAdapter playlistAllAdapter = new PlaylistCursorAdapter(this.getApplicationContext(),
        //                                                                 R.layout.playlist_item, 
        //                                                                 playlistAllCursor, 
        //                                                                 fieldsFrom, 
        //                                                                 fieldsTo);
        PlaylistArrayAdapter playlistAllAdapter = new PlaylistArrayAdapter(getApplicationContext(),
                R.layout.playlist_item, playlistArray);

        /*
         * Create Dialog
         */
        AlertDialog.Builder aD = new AlertDialog.Builder(context);
        aD.create();
        aD.setTitle("Select Playlist");
        aD.setAdapter(playlistAllAdapter, playlistDialogClickListener);
        aD.show();

        //           playlistView.setAdapter(playlistAllAdapter);
        //           
        //           playlistView.setOnItemClickListener(playlistItemClickListener);

        //           /*
        //            * Animate scroll up of the listview
        //            */
        //           TranslateAnimation slideUp = new TranslateAnimation(0,0,display.getHeight(),0);
        //           slideUp.setFillAfter(true);
        //           slideUp.setDuration(250);
        //           playlistContainer.startAnimation(slideUp);
        //           
        //this.mainUIContainer.addView(playlistAllSpinner);
        return true;
    /*
     * Preferences
     */
    case 7:
        if (GRATIS) {
            showLitePopup();
            return true;
        }

        Intent i = new Intent();
        i.setClass(getApplicationContext(), RockOnSettings.class);
        //              this.recentPlaylistPeriod = getSharedPreferences(PREFS_NAME, 0)
        (new RockOnPreferenceManager(FILEX_PREFERENCES_PATH)).getInt(new Constants().PREF_KEY_RECENT_PERIOD,
                new Constants().RECENT_PERIOD_DEFAULT_IN_DAYS);
        startActivityForResult(i, new Constants().PREFERENCES_REQUEST);
        return true;
    /*
     * Help
     */
    case 8:
        this.hideMainUI();
        //this.mainUIContainer.setVisibility(View.GONE);
        this.showHelpUI();
        return true;
    /*
     * Exit
     */
    case 2:
        try {
            this.playerServiceIface.destroy();
            this.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    return false;
}