【问题标题】:Auto suggestion in search view but not using content provider搜索视图中的自动建议,但不使用内容提供者
【发布时间】:2016-06-06 09:33:50
【问题描述】:

我想在我的搜索视图中添加自动建议(不在操作栏中),此建议来自 GET API 调用。我解析对 POJO 类的响应。 现在,是否有必要将建议存储在内容提供者中?或者我可以使用 POJO 类本身,如果可以,如何? 感谢您的帮助。

【问题讨论】:

  • 你能显示你当前的适配器吗?
  • 你在使用“gd.geobytes”接口吗?

标签: android searchview autosuggest


【解决方案1】:

您可以为此使用 Textwatcher..

searchField.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}

        @Override
        public void afterTextChanged(final Editable s) {
            if (s.length() > 2) {
                ExecutorService executorService = Executors.newSingleThreadExecutor();
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url = new URL("api call")
                            // fetch and show the result
                            }
                        } catch (MalformedURLException e) {
                            Log.e(((Object) this).getClass().getCanonicalName(), e.getMessage());
                        } catch (IOException e) {
                            Log.e(((Object) this).getClass().getCanonicalName(), e.getMessage());
                        } catch (JSONException e) {
                            Log.e(((Object) this).getClass().getCanonicalName(), e.getMessage());
                        }


                    }
                });
            }
        }
    });

我想这就是你想要的..

【讨论】:

    【解决方案2】:

    这可能会对您有所帮助:

    FilterEditText.addTextChangedListener(filterTextWatcher);
    
    private TextWatcher filterTextWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }
    
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //hide or show close icon based on text entered
            try {
    
                if (s.length() == 0) {
               //not typed anything in the search box then load the adapter with default values or empty
    
                    initializeAdapter(mAirportCity);
    
    
                } 
            } catch (Exception e) { e.printStackTrace(); }
    
            //If user enters text to search, then call the api with entered text
            onSearchTextChanged(s.toString());
        }
    };
    
    public void onSearchTextChanged(String searchTxt) {
        if (searchTxt == null || TextUtils.isEmpty(searchTxt) || searchTxt.length() < 2) {
            return;
        } else {
            String url = "Your Base URL";
            url= url + searchTxt.trim().replace(" ", "%20");
                CJRVolley.getRequestQueue(getApplicationContext()).add(new CJRGsonGetRequest(url,
                        this, this, "Your Model Class here", null));
    
        }
    }
    
    
     @Override
    public void onResponse(IJRDataModel response) {
    
            //in onresponse initialize the adapter here
        }
        }
    

    【讨论】:

    • 感谢@Naveen 的回复。但我没有使用 textView,我使用的是搜索视图,并且 addTextChangedListener 不适用,setOnQueryTextListener 可以。我的问题是,我在 onQueryTextChange 中调用了自动建议的 API 后,如何显示自动建议?因为它不是 AutoCompleteTextView 并且搜索视图只需要 cursorAdapter 并且我不想使用内容提供程序。再次感谢! :)
    猜你喜欢
    • 2020-06-18
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多