2012-12-24 2 views
8

나는 액션 바에 SearchView을 가지고 있으며, 서비스에서 제안을 가져오고 싶습니다.네트워크에서 안드로이드 searchview 님의 제안

안드로이드에서 샘플을 보았습니다 Samples Searchable Dictionary. 하지만 로컬 데이터베이스에서 결과를로드하고 있습니다.

알았어. Content Provider을 사용해야합니다. 하지만 웹 서비스에서 결과를 가져올 때 궁금합니다. 네트워크 통화를 할 때를 의미합니다. 누구든지 도와 주실 수 있습니다. 당신은 프로젝트 및 이러한 방법이 작동하는 방법 문서에서 볼 수 있듯이 당신이 거기에 구현하는 두 가지 방법

/** 
     * Called when the user submits the query. This could be due to a key press on the 
     * keyboard or due to pressing a submit button. 
     * The listener can override the standard behavior by returning true 
     * to indicate that it has handled the submit request. Otherwise return false to 
     * let the SearchView handle the submission by launching any associated intent. 
     * 
     * @param query the query text that is to be submitted 
     * 
     * @return true if the query has been handled by the listener, false to let the 
     * SearchView perform the default action. 
     */ 
     boolean onQueryTextSubmit(String query); 

     /** 
     * Called when the query text is changed by the user. 
     * 
     * @param newText the new content of the query text field. 
     * 
     * @return false if the SearchView should perform the default action of showing any 
     * suggestions if available, true if the action was handled by the listener. 
     */ 
     boolean onQueryTextChange(String newText); 

수있는 제안을 보여주고 싶은 당신이 searchview를 사용하는 그렇다면

답변

0

. 그래서 사용자가 입력하는 동안 onQueryTextChange 메서드가 호출되며 새 키워드를 입력하거나 삭제할 때마다 호출됩니다.

이 방법에서는 서버를 호출하여 제안을 받고이를 사용자에게 제공 할 수 있습니다.

예를 들어 사용자가 검색하려고하는 경우. "사과"그래서 "app"다음에 "a"다음에 "ap"에 대한 히트 서버와 같은 모든 키워드에 대해 서버를 치는 것은 의미가 없습니다.

따라서 서버를 한 번 치는 것이 낫습니다. 사용자는 핸들러와 runnable을 사용하여 처리 할 수있는 searchview에서 타이핑을 중지합니다.

  Handler mHandler; 
     String mQueryText; 


     @Override 
      public boolean onQueryTextChange(final String newText) { 
       mQueryText=newText; 
       //Make if invisible once you get the data. 
       mProgressBar.setVisibility(View.VISIBLE); 
       mHandler.removeCallbacks(mRunnable); 
       mHandler.postDelayed(mRunnable,200); 
       return true; 
       } 

    private Runnable mRunnable =new Runnable() { 
     @Override 
     public void run() { 

     //Hit server here. 

     } 
    }; 

서버를 때리고 데이터를 가져 오는 동안 진행률 표시 줄이나 다른 것을 표시 할 수 있습니다.

EditText https://developer.android.com/reference/android/widget/EditText.html과 동일한 논리를 적용 할 수 있습니다. 도움이 되길 바랍니다.