2012-10-18 2 views
4

내 앱에서 검색을 구현하려고하지만 검색 결과를 표시하기 위해 별도의 활동을 사용하고 싶지 않습니다. 대신 SearchView 아래에 표시되는 제안 목록 만 사용하고 싶습니다.SearchView로 사용자 정의 검색 구현

SearchView에서 setOnQueryTextListener을 사용할 수 있으며 입력을 수신하고 결과를 검색 할 수 있습니다. 그러나이 결과를 SearchView 아래의 목록에 어떻게 추가 할 수 있습니까? List<String>에서 검색한다고 가정 해 보겠습니다.

+0

샘플 코드 스 니펫을 구현 한 방법대로 제공해 주실 수 있습니까? – Anirudh

답변

2

작성해야 할 내용은 Content Provider입니다. 이렇게하면 사용자가 SearchView에 사용자 정의 결과를 추가하고 사용자가 무언가를 입력 할 때마다 자동 완성 기능을 추가 할 수 있습니다.

내가 잘못 기억하는 경우, 내 프로젝트 중 하나에서 비슷한 것을했는데 너무 오래 걸리지 않았습니다.

나는이 도움이 될 수 있으리라 생각이 도움이 SearchManager - adding custom suggestions

희망 :

그리고이 Turn AutoCompleteTextView into a SearchView in ActionBar instead.

N.은

+0

활동의 동일한 작업 표시 줄에 두 개의 검색 위젯을 추가 할 수 있습니까? –

1

내가 검색 문자열을 취하는 글고 치기를 사용하여 내 응용 프로그램에서 검색을 구현했습니다.
그리고이 EditText 아래에 검색을 수행 할 ListView가 있습니다.

<EditText 
    android:id="@+id/searchInput" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/input_patch" 
    android:gravity="center_vertical" 
    android:hint="@string/search_text" 
    android:lines="1" 
    android:textColor="@android:color/white" 
    android:textSize="16sp" > 
</EditText> 
<ListView 
    android:id="@+id/appsList" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/searchInput" 
    android:cacheColorHint="#00000000" > 
</ListView> 

EditText는 EditText에 입력 된 검색 텍스트에 따라 변경됩니다.

etSearch = (EditText) findViewById(R.id.searchInput); 
etSearch.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     searchList(); 
    } 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 
    @Override 
    public void afterTextChanged(Editable s) { 
    } 
}); 

searchList 함수()는 실제 여기 list

private void searchList() { 
    String s = etSearch.getText().toString(); 
    int textlength = s.length(); 
    String sApp; 
    ArrayList<String> appsListSort = new ArrayList<String>(); 
    int appSize = list.size(); 
    for (int i = 0; i < appSize; i++) { 
     sApp = list.get(i); 
     if (textlength <= sApp.length()) { 
      if (s.equalsIgnoreCase((String) sApp.subSequence(0, textlength))) { 
       appsListSort.add(list.get(i)); 
      } 
     } 
    } 
    list.clear(); 
    for (int j = 0; j < appsListSort.size(); j++) { 
     list.add(appsListSort.get(j)); 
    } 
    adapter.notifyDataSetChanged(); 
} 

찾고 ListView에에 나타내고 adapter리스트 뷰 어댑터이다 ArrayList를 수행하다.
이 방법으로 도움이되기를 바랍니다.

+0

'SearchWidget'을 사용하고 싶습니다. 그리고 그것은 자신의 제안 목록입니다. – nhaarman

+0

sApp = list.get (i);에서 오류가 발생했습니다. 이 문제를 도와주세요. –

+0

@DeepakGupta 오류에 대해 설명해주십시오. – Zeba