0

나는 도시 목록을 가지고있다. 그 목록에서 edittextbox를 입력 할 때 사용자가 자동 ​​제안을 얻길 원합니다. 어떻게 이루어 집니까? Google을 통해 Google을 검색했지만 이에 대한 자습서를 찾을 수 없습니다. 어떤 도움을 주셔서 감사합니다. 감사합니다. .Edittext의 목록보기에서 텍스트 필터링 안드로이드

답변

2

이 작업을 수행하려면 AutoCompleteTextView을 사용해야합니다.

다음 코드는 사용자가 입력하는 동안 여러 나라의 이름을 제안 텍스트 뷰 만드는 방법을 보여줍니다 :

public class CountriesActivity extends Activity { 
    protected void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.countries); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_dropdown_item_1line, COUNTRIES); 
     AutoCompleteTextView textView = (AutoCompleteTextView) 
       findViewById(R.id.countries_list); 
     textView.setAdapter(adapter); 
    } 

    private static final String[] COUNTRIES = new String[] { 
     "Belgium", "France", "Italy", "Germany", "Spain" 
    }; 
} 
+0

감사합니다.하지만이 약간 다르게 싶습니다. 목록에 대한 어댑터가 있습니다. 상위 10 개 cties (정적 목록)입니다. 이제 사용자가 edittext 상자에 입력하면 필자는 다른 도시 목록의 필터를 원합니다. 어떻게하면 될까요? – Namratha

+1

사용자 지정 어댑터를 작성하여 AutoCompleteTextView – Alex

+0

에 전달합니다. edittext 및 listview에서 AutoCompleteTextView를 사용하고 입력 된 텍스트를 목록 어댑터의 필터에 전달하면 어떤 이점이 있습니까? – Namratha

0

다른 목록에서 검색하려면 "Filter"을 사용할 수 있습니다. 자신 만의 필터를 구현하여 [MyFilter extends Filter]를 사용하고 "FilterResults performFiltering (CharSequence prefix)"및 "void publishResults (CharSequence constraint, FilterResults results)"메서드를 재정 의하여 자신의 데이터 (국가 목록)에 고유 한 검색 논리를 구현할 수 있습니다.

"새로운 myFilter" 당신의 "글고" 내부 "onTextChanged()"를 TextWatcher의 방법으로 "TextWatcher를"추가를 반환합니다 어댑터에 getMyFilter() 메소드를 추가, myAdapter를 호출하여 필터를 얻을 수 .getMyFilter() myFilter.filter (myTypedString) ". 그 필터에 전화" ".

여과 한 후"무효 publishResults (CharSequence를 제약, FilterResults 결과) "방법은 전화를받을 것입니다. 그 내부를 실제 변경 어댑터 데이터를 UI 새로 고침

관련 문제