Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从onSuggestionClick中的位置获取值(android中的int位置)_Android - Fatal编程技术网

如何从onSuggestionClick中的位置获取值(android中的int位置)

如何从onSuggestionClick中的位置获取值(android中的int位置),android,Android,我想从onSuggestionClick(int position)中的position参数中获取值。对应于每个选择,在方法中传递一个位置。如何从位置获取字符串值 方法是: public boolean onSuggestionClick(int position) { // Your code here String selectedItem = (String)mAdapter.getItem(position

我想从onSuggestionClick(int position)中的position参数中获取值。对应于每个选择,在方法中传递一个位置。如何从位置获取字符串值

方法是:

 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;
            }
相应的适配器是:

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);
我遇到以下错误:

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


position参数返回所单击项目在显示建议列表中的绝对位置

mAdapter
SimpleCursorAdapter

public SimpleCursorAdapter mAdapter;
下面的代码是获取单击的建议的文本值

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;
    }
});
日志:

搜索用户界面: