Example usage for android.widget CursorAdapter getCursor

List of usage examples for android.widget CursorAdapter getCursor

Introduction

In this page you can find the example usage for android.widget CursorAdapter getCursor.

Prototype

public Cursor getCursor() 

Source Link

Document

Returns the cursor.

Usage

From source file:com.vishwa.pinit.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_action_bar, menu);
    mMenu = menu;//  w w w. j  a  v a  2  s.  com
    mSearchMenuItem = menu.findItem(R.id.action_search);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSearchView = (SearchView) mSearchMenuItem.getActionView();
    mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    mSearchView.setQueryHint("Search for locations...");

    mSearchView.setOnSuggestionListener(new OnSuggestionListener() {

        @Override
        public boolean onSuggestionSelect(int position) {
            return false;
        }

        @Override
        public boolean onSuggestionClick(int position) {
            CursorAdapter adapter = mSearchView.getSuggestionsAdapter();
            Cursor cursor = adapter.getCursor();
            if (cursor != null) {
                if (cursor.moveToPosition(position)) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(
                            Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
                    mSearchMenuItem.collapseActionView();
                    String geolocation = cursor
                            .getString(cursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID));
                    if (geolocation != null && !geolocation.isEmpty()) {
                        double latitude = Double.parseDouble(geolocation.split(",")[0]);
                        double longitude = Double.parseDouble(geolocation.split(",")[1]);
                        LatLng geopoint = new LatLng(latitude, longitude);
                        mMap.animateCamera(
                                CameraUpdateFactory.newCameraPosition(new CameraPosition(geopoint, 17, 0, 0)));
                    }
                }
            }
            return true;
        }
    });

    mSearchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            CursorAdapter adapter = mSearchView.getSuggestionsAdapter();
            Cursor cursor = adapter.getCursor();
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(
                            Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
                    mSearchMenuItem.collapseActionView();
                    String geolocation = cursor
                            .getString(cursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID));
                    if (geolocation != null && !geolocation.isEmpty()) {
                        double latitude = Double.parseDouble(geolocation.split(",")[0]);
                        double longitude = Double.parseDouble(geolocation.split(",")[1]);
                        LatLng geopoint = new LatLng(latitude, longitude);
                        mMap.animateCamera(
                                CameraUpdateFactory.newCameraPosition(new CameraPosition(geopoint, 17, 0, 0)));
                    }
                }
            }
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

    if (!mHasInternet) {
        hideNonRefreshMenuItems();
    } else {
        showNonRefreshMenuItems();
    }
    return true;
}