How to get value from position at onSuggestionClick (int position) in android

I want to get the value from the position parameter in onSuggestionClick (int position). The position is passed in the method according to each choice. How do I get a string value from a position?

Method:

 public boolean onSuggestionClick(int position)
            {
                // Your code here
            String selectedItem = (String)mAdapter.getItem(position);
            Toast.makeText(getBaseContext()," on suggestion click position and item is" + position + selectedItem, Toast.LENGTH_LONG).show();


                startActivity(new Intent(getBaseContext(), SearchResultsActivity.class));

                return true;
            }

      

Matching adapter:

final String[] from = new String[] {"cityName"};
        final int[] to = new int[] {android.R.id.text1};
        mAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2,
                null,
                from,
                to,
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        search.setSuggestionsAdapter(mAdapter);

      

I am getting the following error:

java.io.IOException at android.accounts.AccountManager.convertErrorToException

+3


source to share


1 answer


Parameter

position returns the absolute position of the clicked item in the list of displayed suggestions.

mAdapter

is an object SimpleCursorAdapter

public SimpleCursorAdapter mAdapter;

      

Below code should get the text value of the clicked sentence



SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchView.setIconifiedByDefault(false);
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSuggestionsAdapter(mAdapter);
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
    @Override
    public boolean onSuggestionClick(int position) {
        Cursor cursor = mAdapter.getCursor();
        cursor.moveToPosition( position );
        columnIndex = 1 /* Set your column index*/
        String selectedItem = cursor.getString(columnIndex);
        Log.d("Clicked Item", selectedItem);
        return true;
    }

    @Override
    public boolean onSuggestionSelect(int position) {
        /* Write your code */
        return true;
    }
});

      

Logcat: LogCat Print

Search user interface: Search UI

0


source







All Articles